[R] Axiom alike formula needs to get prooved
Hi, I found the following relationship: it is given x is a vector, bm1 and bm2 are vectors of type logical end exactly as long as x then the following formula returns alwasy TRUE: all( x[ bm1 & bm2] == x[ bm2][ bm1[ bm2]] ) . I think it is no such great deal and a lot of R programmers will know this and use it in their code intutively. But the point is: Years ago I tried to prove this relationship and found no acceptable solution for that. Is anyone able to prove this in a mathematical correct way? Besides that: here is another one it is given a dataframe 'result_df' with at least the column 'result_value' best_results <- with( result_df, sort( result_value))[1:100] # lower is better best_results_bmask <- with( result_df, result_value %in% best_results) best_results_order2 <- with( result_df[ best_results_bmask, ], order( result_value)) df1 <- result_df[best_results_order, ][ best_results_bmask[best_results_order], ] df2 <- result_df[best_results_bmask, ][best_results_order2, ] all( df1 == df2) # that was the easy part, now this: df3 <- result_df[best_results_order[ best_results_bmask[ best_results_order]], ] all( df1 == df3) # it works, but how can I understand that and how it is proven? Regards Frank Schwidom __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Open a file which name contains a tilde
Hi John, First, the unix and linux filesystem allows the use of any nonzero character in its filesystem filenames and the c functions open / fopen, symlink. rename, chdir and so on don't care about any tilde. If the open systemcall gets a file which begins with a tilde then it will try to open this filename without any preceding modification. So the tilde expansion is not really a unix idiom. It is rather an idiom of its unix shells like bash. But beware, the bash treats the tilde as home path not in all cases. Inside of quotes the tilde keeps unaltered. So bedides any cenvenience functionality which can lead to failing code the bash has a very clean way to access files in a unique way. And here is the problem wirh R. The R shell has no quoting mechanism for this use case. It provides the tilde expansion after a string has been defined. In the bash it is the opposite. First the string gets defined ( for instance ~"/abc.txt" would expand to "/home/user/abc.txt" or "~/abc.txt" keeps "~/abc.txt" and that means a directory named '~' with a directory entry named 'abc.txt' inside of it) and after it is defined it keeps consistent and can be passed to every function. Actually R has already a clean string datatype. But by the transition to filesystem operations the expansion takes place. In my opinion at the wrong place. And additionally there exists no quoting mechanism which allows to keep tildes unaltered. It would be better to clear distinguish between a string which is passed to the filesystem calls and some convenience expansion mechanisms. But it is like it is. For my need I created a solution which takes strings unaltered and so I can fulfil my work. And for whom who is interested in it, can take a look: https://github.com/schwidom/simplefs Suggestions are welcome. Kind regards Frank Schwidom On 2019-06-12 12:50:12, John via R-help wrote: > On Wed, 5 Jun 2019 18:07:15 +0200 > Frank Schwidom wrote: > > In reading file names, names with spaces require escaping of the > spaces, and you are using not only a tilde but the space as spell. The > tilde is conventionally read as wild card representing the leading > portion of the file address, which is what you see here: > > The long standing convention in unix and linux is that a tilde is > shorthand for your home directory, just as "./" is the local directory > and "../" is one step back up the directory tree. The output of > path.expand() varies in how your string is constructed: > > 1. > path.expand("ab") >[1] "ab" > 2. > path.expand("a b") >[1] "a b" > 3. > path.expand("a ~b") >[1] "a ~b" > 4. > path.expand("a ~ b") >[1] "a /home/john b" > 5. > path.expand("a ~/b") >[1] "a /home/john/b" > 6. > path.expand("~/a b") >[1] "/home/john/a b" > > Notice that the spaces have an effect on how the tilde is parsed in > the string. The next to last case sees a string with three elements: > "a", the local path, and "b". The last expands the tilde as the "path" > to a hypothetical file "b" in the home directory. In the sixth case the > same behaviour occurs. > > If you read the help for path.expand(), the routine expects > "leading" tildes, which explains the free floating "path in the fourth > example. In the third example, where the forward slash is omitted, the > routine simply treats the tilde as part of a string. > > Also note this at http://www.linfo.org/file_name.html: > > "...file names only use alphanumeric characters (mostly lower case), > underscores, hyphens and periods. Other characters, such as dollar > signs, percentage signs and brackets, have special meanings to the > shell and can be distracting to work with. File names should never > begin with a hyphen." > > The probable "bug" is that none of the programmers imagined that anyone > would use special characters within file names, "legal" or not, (just > not done, don't y' know). In any case the simple solution, if you > really, really need a tilde in a filename, is to avoid setting it off in > spaces, or any other character that could mislead a routine into > treating it conventionally as shorthand for you home directory. > > JWDougherty > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] [Rd] Open a file which name contains a tilde
Hi Gabriel, I actually want to make renames over thousands of files. But if I am not able to express the source filename of the rename operation I will not be able to get the work done. Besides the fact that there are issues I think that R is qualified for solving my problem by the method how it can handle long vectors of strings, booleans and also lists. Kind regards, Frank On 2019-06-11 09:49:17, Gabriel Becker wrote: >Hi Frank, >I'm hesitant to be "that guy", but in case no one else has brought this up >to you, having files with a tilde in their names (generally but especially >on a linux system, where ~ in file names has a very important special >meaning in some cases, as we know) strikes me as an exceptionally bad >practice anyway. In light of that, the solution with the smallest amount >of pain for you is almost surely to just... not do that. Your filenames >will be better for it anyway. >There is a reason no one has complained about this before, and while I >haven't run a study or anything, I strongly suspect its that "everyone" >else is already on the "no tildes in filenames" bandwagon, so this >behavior, even if technically a bug, has no ability to cause them > problems. >Best, >~G >On Tue, Jun 11, 2019 at 8:25 AM Frank Schwidom <[1]schwi...@gmx.net> >wrote: > > Hi, > > yes, I have seen this package and it has the same tilde expanding > problem. > > Please excuse me I will cc this answer to r-help and r-devel to keep the > discussion running. > > Kind regards, > Frank Schwidom > > On 2019-06-11 09:12:36, Gábor Csárdi wrote: > > Just in case, have you seen the fs package? > > [2]https://fs.r-lib.org/ > > > > Gabor > > > > On Tue, Jun 11, 2019 at 7:51 AM Frank Schwidom <[3]schwi...@gmx.net> > wrote: > > > > > > Hi, > > > > > > to get rid of any possible filename modification I started a little > project to cover my usecase: > > > > > > [4]https://github.com/schwidom/simplefs > > > > > > This is my first R package, suggestions and a review are welcome. > > > > > > Thanks in advance > > > Frank Schwidom > > > > > > On 2019-06-07 09:04:06, Richard O'Keefe wrote: > > > > How can expanding tildes anywhere but the beginning of a file > name NOT be > > > > considered a bug? > > > > On Thu, 6 Jun 2019 at 23:04, Ivan Krylov > <[1][5]krylov.r...@gmail.com> wrote: > > > > > > > > On Wed, 5 Jun 2019 18:07:15 +0200 > > > > Frank Schwidom <[2][6]schwi...@gmx.net> wrote: > > > > > > > > > +> path.expand("a ~ b") > > > > > [1] "a /home/user b" > > > > > > > > > How can I switch off any file crippling activity? > > > > > > > > It doesn't seem to be possible if readline is enabled and > works > > > > correctly. > > > > > > > > Calls to path.expand [1] end up [2] in R_ExpandFileName [3], > which > > > > calls R_ExpandFileName_readline [4], which uses libreadline > function > > > > tilde_expand [5]. tilde_expand seems to be designed to expand > '~' > > > > anywhere in the string it is handed, i.e. operate on whole > command > > > > lines, not file paths. > > > > > > > > I am taking the liberty of Cc-ing R-devel in case this can be > > > > considered a bug. > > > > > > > > -- > > > > Best regards, > > > > Ivan > > > > > > > > [1] > > > > > > [3][7]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/names.c#L807 > > > > > > > > [2] > > > > > > [4][8]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/platform.c#L1915 > > > > > > > > [3] > > > > > > [5][9]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-unix.c#L147 > > > > > > &
Re: [R] [Rd] Open a file which name contains a tilde
Hi, yes, I have seen this package and it has the same tilde expanding problem. Please excuse me I will cc this answer to r-help and r-devel to keep the discussion running. Kind regards, Frank Schwidom On 2019-06-11 09:12:36, Gábor Csárdi wrote: > Just in case, have you seen the fs package? > https://fs.r-lib.org/ > > Gabor > > On Tue, Jun 11, 2019 at 7:51 AM Frank Schwidom wrote: > > > > Hi, > > > > to get rid of any possible filename modification I started a little project > > to cover my usecase: > > > > https://github.com/schwidom/simplefs > > > > This is my first R package, suggestions and a review are welcome. > > > > Thanks in advance > > Frank Schwidom > > > > On 2019-06-07 09:04:06, Richard O'Keefe wrote: > > >How can expanding tildes anywhere but the beginning of a file name NOT > > > be > > >considered a bug? > > >On Thu, 6 Jun 2019 at 23:04, Ivan Krylov <[1]krylov.r...@gmail.com> > > > wrote: > > > > > > On Wed, 5 Jun 2019 18:07:15 +0200 > > > Frank Schwidom <[2]schwi...@gmx.net> wrote: > > > > > > > +> path.expand("a ~ b") > > > > [1] "a /home/user b" > > > > > > > How can I switch off any file crippling activity? > > > > > > It doesn't seem to be possible if readline is enabled and works > > > correctly. > > > > > > Calls to path.expand [1] end up [2] in R_ExpandFileName [3], which > > > calls R_ExpandFileName_readline [4], which uses libreadline function > > > tilde_expand [5]. tilde_expand seems to be designed to expand '~' > > > anywhere in the string it is handed, i.e. operate on whole command > > > lines, not file paths. > > > > > > I am taking the liberty of Cc-ing R-devel in case this can be > > > considered a bug. > > > > > > -- > > > Best regards, > > > Ivan > > > > > > [1] > > > > > > [3]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/names.c#L807 > > > > > > [2] > > > > > > [4]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/platform.c#L1915 > > > > > > [3] > > > > > > [5]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-unix.c#L147 > > > > > > [4] > > > > > > [6]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-std.c#L494 > > > > > > [5] > > > > > > [7]https://git.savannah.gnu.org/cgit/readline.git/tree/tilde.c?h=devel#n187 > > > > > > __ > > > [8]R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > [9]https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide > > > [10]http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > > > > > References > > > > > >Visible links > > >1. mailto:krylov.r...@gmail.com > > >2. mailto:schwi...@gmx.net > > >3. > > > https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/names.c#L807 > > >4. > > > https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/platform.c#L1915 > > >5. > > > https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-unix.c#L147 > > >6. > > > https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-std.c#L494 > > >7. > > > https://git.savannah.gnu.org/cgit/readline.git/tree/tilde.c?h=devel#n187 > > >8. mailto:R-help@r-project.org > > >9. https://stat.ethz.ch/mailman/listinfo/r-help > > > 10. http://www.r-project.org/posting-guide.html > > > > __ > > r-de...@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Open a file which name contains a tilde
Hi, to get rid of any possible filename modification I started a little project to cover my usecase: https://github.com/schwidom/simplefs This is my first R package, suggestions and a review are welcome. Thanks in advance Frank Schwidom On 2019-06-07 09:04:06, Richard O'Keefe wrote: >How can expanding tildes anywhere but the beginning of a file name NOT be >considered a bug? >On Thu, 6 Jun 2019 at 23:04, Ivan Krylov <[1]krylov.r...@gmail.com> wrote: > > On Wed, 5 Jun 2019 18:07:15 +0200 > Frank Schwidom <[2]schwi...@gmx.net> wrote: > > > +> path.expand("a ~ b") > > [1] "a /home/user b" > > > How can I switch off any file crippling activity? > > It doesn't seem to be possible if readline is enabled and works > correctly. > > Calls to path.expand [1] end up [2] in R_ExpandFileName [3], which > calls R_ExpandFileName_readline [4], which uses libreadline function > tilde_expand [5]. tilde_expand seems to be designed to expand '~' > anywhere in the string it is handed, i.e. operate on whole command > lines, not file paths. > > I am taking the liberty of Cc-ing R-devel in case this can be > considered a bug. > > -- > Best regards, > Ivan > > [1] > > [3]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/names.c#L807 > > [2] > > [4]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/platform.c#L1915 > > [3] > > [5]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-unix.c#L147 > > [4] > > [6]https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-std.c#L494 > > [5] > > [7]https://git.savannah.gnu.org/cgit/readline.git/tree/tilde.c?h=devel#n187 > > __ > [8]R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > [9]https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > [10]http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > References > >Visible links >1. mailto:krylov.r...@gmail.com >2. mailto:schwi...@gmx.net >3. > https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/names.c#L807 >4. > https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/platform.c#L1915 >5. > https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-unix.c#L147 >6. > https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-std.c#L494 >7. https://git.savannah.gnu.org/cgit/readline.git/tree/tilde.c?h=devel#n187 >8. mailto:R-help@r-project.org >9. https://stat.ethz.ch/mailman/listinfo/r-help > 10. http://www.r-project.org/posting-guide.html __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Open a file which name contains a tilde
On 2019-06-05 20:32:07, Enrico Schumann wrote: > >>>>> "FS" == Frank Schwidom writes: > > FS> Hi, > FS> As I can see via path.expand a filename which contains a tilde > anywhere gets automatically crippled. > > FS> +> path.expand("a ~ b") > FS> [1] "a /home/user b" > > FS> +> path.expand("a ~ b ~") > FS> [1] "a /home/user b /home/user" > > FS> I want to open a file regardless whether its name contains any > character unless 0. > > FS> The unix filesystem allow the creation of such files, it sould be > possible to open these. > > FS> How can I switch off any file crippling activity? > > FS> Kind regards, > FS> Frank > > Do you need 'path.expand'? For example, > > readLines("~/Desktop/a ~ b") > > reads just fine the content of a file named > 'a ~ b' on my desktop. > > > -- > Enrico Schumann > Lucerne, Switzerland > http://enricoschumann.net > Appendix: I found out in the meantime that I can use 'R --no-readline' but I want to use readline and I found no possible readline configuration /etc/inputrc). And maybe it works as Rscript. But that should be more consistent because it is in fact very basic. Kind regards, Frank __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Open a file which name contains a tilde
On 2019-06-05 20:32:07, Enrico Schumann wrote: > >>>>> "FS" == Frank Schwidom writes: > > FS> Hi, > FS> As I can see via path.expand a filename which contains a tilde > anywhere gets automatically crippled. > > FS> +> path.expand("a ~ b") > FS> [1] "a /home/user b" > > FS> +> path.expand("a ~ b ~") > FS> [1] "a /home/user b /home/user" > > FS> I want to open a file regardless whether its name contains any > character unless 0. > > FS> The unix filesystem allow the creation of such files, it sould be > possible to open these. > > FS> How can I switch off any file crippling activity? > > FS> Kind regards, > FS> Frank > > Do you need 'path.expand'? For example, > > readLines("~/Desktop/a ~ b") > > reads just fine the content of a file named > 'a ~ b' on my desktop. > > > -- > Enrico Schumann > Lucerne, Switzerland > http://enricoschumann.net > Thanks for yor answer. $ echo 123 > ~/'a ~ b'.txt ls ~/'a ~ b'.txt /home/user/a ~ b.txt +> readLines("~/a ~ b.txt") Error in file(con, "r") : cannot open the connection In addition: Warning message: In file(con, "r") : cannot open file '/home/user/a /home/user b.txt': No such file or directory +> version _ platform x86_64-pc-linux-gnu arch x86_64 os linux-gnu system x86_64, linux-gnu status major 3 minor 1.1 year 2014 month 07 day10 svn rev66115 language R version.string R version 3.1.1 (2014-07-10) nickname Sock it to Me Kind regards, Frank __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Open a file which name contains a tilde
Hi, As I can see via path.expand a filename which contains a tilde anywhere gets automatically crippled. +> path.expand("a ~ b") [1] "a /home/user b" +> path.expand("a ~ b ~") [1] "a /home/user b /home/user" I want to open a file regardless whether its name contains any character unless 0. The unix filesystem allow the creation of such files, it sould be possible to open these. How can I switch off any file crippling activity? Kind regards, Frank __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R lappy, sapply or mapply question
On 2015-10-09 20:15:16, liqunhan--- via R-help wrote: > for (k in 1 : 5) { > xlist <- list(x5 = dailyrecord$a[k], x6 = dailyrecord$e[k], x7 = > dailyrecord$f[k]) > result_forloop[k] <- fun3(list1, list2, xlist) > } result_forloop <- lapply( 1 : 5, function( k) { tmpRow <- dailyrecord[ k, ] xlist <- with( df, list( x5= a, x6= e, x7= f)) fun3(list1, list2, xlist) }) __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R lappy, sapply or mapply question
correction: On 2015-10-10 16:08:39, Frank Schwidom wrote: > On 2015-10-09 20:15:16, liqunhan--- via R-help wrote: > > > for (k in 1 : 5) { > > xlist <- list(x5 = dailyrecord$a[k], x6 = dailyrecord$e[k], x7 = > > dailyrecord$f[k]) > > result_forloop[k] <- fun3(list1, list2, xlist) > > } > > result_forloop <- lapply( 1 : 5, function( k) { > > tmpRow <- dailyrecord[ k, ] > > xlist <- with( tmpRow, list( x5= a, x6= e, x7= f)) > > fun3(list1, list2, xlist) > > }) > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] merging tables based on both row and column names
test1 <- (rbind(c(0.1,0.2),0.3,0.1)) rownames(test1)=c('y1','y2','y3') colnames(test1) = c('x1','x2'); test2 <- (rbind(c(0.8,0.9,0.5),c(0.5,0.1,0.6))) rownames(test2) = c('y2','y5') colnames(test2) = c('x1','x3','x2') lTest12 <- list( test1, test2) namesRow <- unique( unlist( lapply( lTest12, rownames))) namesCol <- unique( unlist( lapply( lTest12, colnames))) tmp1 <- do.call( cbind, lapply( lTest12, function( x) as.vector( x[ match( namesRow, rownames( x)), match( namesCol, colnames( x))]))) tmp2 <- apply( tmp1, 1, list) tmp3 <- lapply( tmp2, function( x) x[[1]][ !is.na( x[[1]])]) dimnames1 <- list( namesRow, namesCol) tmp4 <- array( data= tmp3, dim= sapply( dimnames1, length), dimnames= dimnames1) paste( tmp4) [1] "0.1" "c(0.3, 0.8)" "0.1" "0.5" "0.2" [6] "c(0.3, 0.5)" "0.1" "0.6" "numeric(0)" "0.9" [11] "numeric(0)" "0.1" tmp4 x1x2x3 y1 0.1 0.2 Numeric,0 y2 Numeric,2 Numeric,2 0.9 y3 0.1 0.1 Numeric,0 y5 0.5 0.6 0.1 Regards. On 2015-09-28 18:46:18, C Lin wrote: > Dear R users, > > I am trying to merge tables based on both their row names and column names. > My ultimate goal is to build a distribution table of values for each > combination of row and column names. > I have more test tables, more x's and y's than in the toy example below. > Thanks in advance for your help. > > For example : > test1 <- data.frame(rbind(c(0.1,0.2),0.3,0.1)) > rownames(test1)=c('y1','y2','y3') > colnames(test1) = c('x1','x2'); > test2 <- data.frame(rbind(c(0.8,0.9,0.5),c(0.5,0.1,0.6))) > rownames(test2) = c('y2','y5') > colnames(test2) = c('x1','x3','x2') > > test1 >x1 x2 > y1 0.1 0.2 > y2 0.3 0.3 > y3 0.1 0.1 > > test2 >x1 x3 x2 > y2 0.8 0.9 0.5 > y5 0.5 0.1 0.6 > > I would like to combine test1 and test2 such that if the column name and row > name are both the same they are combined. > > combined_test >x1 x2 x3 > y1 0.1 0.2 NA > y2 (0.3,0.8)(0.3,0.5) 0.9 > y3 0.1 0.1 NA > y5 0.5 0.6 0.1 > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] vector manipulations -- differences
And if we want to use the approach of William Dunlap for sequence.optimization , then we can write: rev( xr[ seq_len(sum(vec)) - rep.int(cumsum(c(0L, vec[-length(vec)])), vec)] - rep.int( xr[ -1], vec)) Regards. On 2015-09-22 23:43:10, Frank Schwidom wrote: > Hi, > > xr <- rev( x) > vec <- 1:(length( x) - 1) > rev( xr[ sequence( vec)] - rep.int( xr[ -1], vec)) > > > On 2015-09-21 14:17:40, Dan D wrote: > > I need an efficient way to build a new n x (n-1)/2 vector from an n-vector x > > as: > > > > c(x[-1]-x[1], x[-(1:2)]-x[2], ... , x[-(1:(n-1)] - x[n-1]) > > > > x is increasing with x[1] = 0. > > > > The following works but is not the greatest: > > junk<-outer(x, x, '-') > > junk[junk>0] > > > > e.g., > > given > > x<-c(0, 3, 7, 20) > > junk<-outer(x, x, '-') > > junk[junk>0] # yields: c(3, 7, 20, 4, 17, 13) as needed, but it has to go > > through > > junk > > # [,1] [,2] [,3] [,4] > > #[1,]0 -3 -7 -20 > > #[2,]30 -4 -17 > > #[3,]740 -13 > > #[4,] 20 17 130 > > > > Anyone have a better idea? > > > > -Dan > > > > > > > > -- > > View this message in context: > > http://r.789695.n4.nabble.com/vector-manipulations-differences-tp4712575.html > > Sent from the R help mailing list archive at Nabble.com. > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] c(1:n, 1:(n-1), 1:(n-2), ... , 1)
Hi I have to correct myself, this last solution is not universally valid here a better one: tmp1 <- ( 1 - outer( max( x):1, x, FUN='-')) tmp1[ tmp1 > 0] On 2015-09-17 21:06:30, Frank Schwidom wrote: > > how abount a more complicated one? > > outer( 1:5, 1:5, '-')[ outer( 1:5, 1:5, '>')] > [1] 1 2 3 4 1 2 3 1 2 1 > > > On Thu, Sep 17, 2015 at 11:52:27AM -0700, David Winsemius wrote: > > You can add this to the list of options to be tested, although my bet would > > be placed on `sequence(5:1)`: > > > > > Reduce( function(x,y){c( 1:y, x)}, 1:5) > > [1] 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 > > > > > > On Sep 17, 2015, at 11:40 AM, Achim Zeileis wrote: > > > > > On Thu, 17 Sep 2015, Peter Langfelder wrote: > > > > > >> Not sure if this is slicker or easier to follow than your solution, > > >> but it is shorter :) > > >> > > >> do.call(c, lapply(n:1, function(n1) 1:n1)) > > > > > > Also not sure about efficiency but somewhat shorter... > > > unlist(lapply(5:1, seq)) > > > > > >> Peter > > >> > > >> On Thu, Sep 17, 2015 at 11:19 AM, Dan D <ddalth...@usgs.gov> wrote: > > >>> Can anyone think of a slick way to create an array that looks like > > >>> c(1:n, > > >>> 1:(n-1), 1:(n-2), ... , 1)? > > >>> > > >>> The following works, but it's inefficient and a little hard to follow: > > >>> n<-5 > > >>> junk<-array(1:n,dim=c(n,n)) > > >>> junk[((lower.tri(t(junk),diag=T)))[n:1,]] > > >>> > > >>> Any help would be greatly appreciated! > > >>> > > >>> -Dan > > >>> > > >>> > > > > David Winsemius > > Alameda, CA, USA > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] vector manipulations -- differences
Hi, xr <- rev( x) vec <- 1:(length( x) - 1) rev( xr[ sequence( vec)] - rep.int( xr[ -1], vec)) On 2015-09-21 14:17:40, Dan D wrote: > I need an efficient way to build a new n x (n-1)/2 vector from an n-vector x > as: > > c(x[-1]-x[1], x[-(1:2)]-x[2], ... , x[-(1:(n-1)] - x[n-1]) > > x is increasing with x[1] = 0. > > The following works but is not the greatest: > junk<-outer(x, x, '-') > junk[junk>0] > > e.g., > given > x<-c(0, 3, 7, 20) > junk<-outer(x, x, '-') > junk[junk>0] # yields: c(3, 7, 20, 4, 17, 13) as needed, but it has to go > through > junk > # [,1] [,2] [,3] [,4] > #[1,]0 -3 -7 -20 > #[2,]30 -4 -17 > #[3,]740 -13 > #[4,] 20 17 130 > > Anyone have a better idea? > > -Dan > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/vector-manipulations-differences-tp4712575.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Extract from data.frame
year <- df$Year[ which.max( df$Amount)] df[ df$Year %in% (as.numeric( as.character( year)) + -1:1), ] Year Amount 2 2002120 3 2003175 4 2004160 On Mon, Sep 21, 2015 at 04:52:46PM +0200, Nico Gutierrez wrote: > Hi All, > > I need to do the following operation from data.frame: > > df <- data.frame(Year = c("2001", "2002", "2003", "2004", "2005", "2006", > "2007"), Amount = c(150, 120, 175, 160, 120, 105, 135)) > df[which.max(df$Amount),] #to extract row with max Amount. > > Now I need to do 3 years average around the max Amount value (ie: > mean(120,175,160)) > > Thanks! > N > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Extract from data.frame
better ( if year is an vector of more than 1 element): df[ df$Year %in% outer(as.numeric( as.character( year)), -1:1, FUN='+'), ] Year Amount 2 2002120 3 2003175 4 2004160 On Mon, Sep 21, 2015 at 10:49:34PM +0200, Frank Schwidom wrote: > > year <- df$Year[ which.max( df$Amount)] > df[ df$Year %in% (as.numeric( as.character( year)) + -1:1), ] > Year Amount > 2 2002120 > 3 2003175 > 4 2004160 > > > On Mon, Sep 21, 2015 at 04:52:46PM +0200, Nico Gutierrez wrote: > > Hi All, > > > > I need to do the following operation from data.frame: > > > > df <- data.frame(Year = c("2001", "2002", "2003", "2004", "2005", "2006", > > "2007"), Amount = c(150, 120, 175, 160, 120, 105, 135)) > > df[which.max(df$Amount),] #to extract row with max Amount. > > > > Now I need to do 3 years average around the max Amount value (ie: > > mean(120,175,160)) > > > > Thanks! > > N > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plot with R un html5
Hi, when you can plot this graph using the rgl-package, then you can use "rgl::writeWebGL" to create an 3D-View in the Browser. Regards On Sat, Sep 19, 2015 at 04:42:47PM +0200, bgnumis bgnum wrote: > Hi al, > > I want to put a graph in a html5 webpage plotted with R (I want to get dar > from Google finance). Is it posible? > > F.e NYSE:C > > Does everybody know how to plot (with code) un html5 to plot trough R o > directly plot the sale plot with monthly data? > > I Hope you can help me explosing an example. > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] aggregate counting variable factors
Hi where can i find 'melt' and 'dcast' ? Regards On Thu, Sep 17, 2015 at 08:22:10AM +, PIKAL Petr wrote: > Hi > > > -Original Message- > > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Kai Mx > > Sent: Wednesday, September 16, 2015 10:43 PM > > To: r-help mailing list > > Subject: [R] aggregate counting variable factors > > > > Hi everybody, > > > > >From a questionnaire, I have a dataset like this one with some 40 > > items: > > > > df1 <- data.frame(subject=c('user1','user2', 'user3', 'user4'), > > item1=c(0,1,2,5), item2=c(1,2,1,2), item3=c(2,3,4,0), item4=c(0,3,3,2), > > item5=c(5,5,5,5)) > > > > Users can choose an answer from 0 to 5 for each item. > > > > Now I want to reshape the dataset to have the items in rows and the > > count > > of each of the result factors in columns: > > > > result <- data.frame (item=c("item1", "item2", "item3", "item4", > > "item5"), > > result0=c(1,0,1,1,0), result1=c(1,2,0,0,0), result2=c(1,2,1,1,0), > > result3=c(0,0,1,2,0), result4=c(0,0,1,0,0), result5=c(1,0,0,0,4)) > > > > I have been fiddling around with melt/plyr, but haven't been able to > > figure > > it out. What's the most elegant way to do this (preferably without > > typing > > in all the item names). > > Perhaps, > > m<-melt(df1) > m$value<-paste("res",m$value, sep="") > dcast(m, variable~value) > Aggregation function missing: defaulting to length > variable res0 res1 res2 res3 res4 res5 > 1item1111001 > 2item2022000 > 3item3101110 > 4item4101200 > 5item5000004 > > Cheers > Petr > > > > > > Thanks so much! > > > > Best, > > > > Kai > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou > určeny pouze jeho adresátům. > Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně > jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze > svého systému. > Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email > jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. > Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či > zpožděním přenosu e-mailu. > > V případě, že je tento e-mail součástí obchodního jednání: > - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, > a to z jakéhokoliv důvodu i bez uvedení důvodu. > - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; > Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany > příjemce s dodatkem či odchylkou. > - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným > dosažením shody na všech jejích náležitostech. > - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost > žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně > pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu > případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je > adresátovi či osobě jím zastoupené známá. > > This e-mail and any documents attached to it may be confidential and are > intended only for its intended recipients. > If you received this e-mail by mistake, please immediately inform its sender. > Delete the contents of this e-mail with all attachments and its copies from > your system. > If you are not the intended recipient of this e-mail, you are not authorized > to use, disseminate, copy or disclose this e-mail in any manner. > The sender of this e-mail shall not be liable for any possible damage caused > by modifications of the e-mail or by delay with transfer of the email. > > In case that this e-mail forms part of business dealings: > - the sender reserves the right to end negotiations about entering into a > contract in any time, for any reason, and without stating any reasoning. > - if the e-mail contains an offer, the recipient is entitled to immediately > accept such offer; The sender of this e-mail (offer) excludes any acceptance > of the offer on the part of the recipient containing any amendment or > variation. > - the sender insists on that the respective contract is concluded only upon > an express mutual agreement on all its aspects. > - the sender of this e-mail informs that he/she is not authorized to enter > into any contracts on behalf of the company except for cases in which he/she > is expressly authorized to do so in writing, and such authorization or power > of attorney is
Re: [R] aggregate counting variable factors
Hi res <- sapply( df1[ , -1], function( x) table(x)[as.character( 0:5)]) rownames( res) <- paste( sep='', 'result', 0:5) res[ is.na( res)] <- 0 res item1 item2 item3 item4 item5 result0 1 0 1 1 0 result1 1 2 0 0 0 result2 1 2 1 1 0 result3 0 0 1 2 0 result4 0 0 1 0 0 result5 1 0 0 0 4 t( res) result0 result1 result2 result3 result4 result5 item1 1 1 1 0 0 1 item2 0 2 2 0 0 0 item3 1 0 1 1 1 0 item4 1 0 1 2 0 0 item5 0 0 0 0 0 4 Regards On Wed, Sep 16, 2015 at 10:43:16PM +0200, Kai Mx wrote: > Hi everybody, > > >From a questionnaire, I have a dataset like this one with some 40 items: > > df1 <- data.frame(subject=c('user1','user2', 'user3', 'user4'), > item1=c(0,1,2,5), item2=c(1,2,1,2), item3=c(2,3,4,0), item4=c(0,3,3,2), > item5=c(5,5,5,5)) > > Users can choose an answer from 0 to 5 for each item. > > Now I want to reshape the dataset to have the items in rows and the count > of each of the result factors in columns: > > result <- data.frame (item=c("item1", "item2", "item3", "item4", "item5"), > result0=c(1,0,1,1,0), result1=c(1,2,0,0,0), result2=c(1,2,1,1,0), > result3=c(0,0,1,2,0), result4=c(0,0,1,0,0), result5=c(1,0,0,0,4)) > > I have been fiddling around with melt/plyr, but haven't been able to figure > it out. What's the most elegant way to do this (preferably without typing > in all the item names). > > Thanks so much! > > Best, > > Kai > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] c(1:n, 1:(n-1), 1:(n-2), ... , 1)
sequence( 5:1) Regards. On Thu, Sep 17, 2015 at 11:19:05AM -0700, Dan D wrote: > Can anyone think of a slick way to create an array that looks like c(1:n, > 1:(n-1), 1:(n-2), ... , 1)? > > The following works, but it's inefficient and a little hard to follow: > n<-5 > junk<-array(1:n,dim=c(n,n)) > junk[((lower.tri(t(junk),diag=T)))[n:1,]] > > Any help would be greatly appreciated! > > -Dan > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/c-1-n-1-n-1-1-n-2-1-tp4712390.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] c(1:n, 1:(n-1), 1:(n-2), ... , 1)
how abount a more complicated one? outer( 1:5, 1:5, '-')[ outer( 1:5, 1:5, '>')] [1] 1 2 3 4 1 2 3 1 2 1 On Thu, Sep 17, 2015 at 11:52:27AM -0700, David Winsemius wrote: > You can add this to the list of options to be tested, although my bet would > be placed on `sequence(5:1)`: > > > Reduce( function(x,y){c( 1:y, x)}, 1:5) > [1] 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 > > > On Sep 17, 2015, at 11:40 AM, Achim Zeileis wrote: > > > On Thu, 17 Sep 2015, Peter Langfelder wrote: > > > >> Not sure if this is slicker or easier to follow than your solution, > >> but it is shorter :) > >> > >> do.call(c, lapply(n:1, function(n1) 1:n1)) > > > > Also not sure about efficiency but somewhat shorter... > > unlist(lapply(5:1, seq)) > > > >> Peter > >> > >> On Thu, Sep 17, 2015 at 11:19 AM, Dan Dwrote: > >>> Can anyone think of a slick way to create an array that looks like c(1:n, > >>> 1:(n-1), 1:(n-2), ... , 1)? > >>> > >>> The following works, but it's inefficient and a little hard to follow: > >>> n<-5 > >>> junk<-array(1:n,dim=c(n,n)) > >>> junk[((lower.tri(t(junk),diag=T)))[n:1,]] > >>> > >>> Any help would be greatly appreciated! > >>> > >>> -Dan > >>> > >>> > > David Winsemius > Alameda, CA, USA > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R WRONG CALCULATIONS - Please Help
On Mon, Sep 14, 2015 at 04:11:57PM +0100, JORGE COLACO wrote: > > X-mean > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] -0.8 -0.2 0.8 -1.2 0.8 -0.2 > [2,] -0.2 0.2 0.8 0.8 0.8 -1.2 > [3,] -1.2 -0.2 -0.8 0.8 -0.2 0.8 > [4,] 0.8 0.8 -1.2 -0.8 -0.2 -0.2 > [5,] -0.2 -0.2 0.8 0.8 -0.8 0.8 > > try this: X - mean[ col( X)] [,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.8 -0.2 0.8 -1.2 0.8 -0.2 [2,] 0.2 -0.2 0.8 0.8 0.8 -1.2 [3,] -0.8 -0.2 -1.2 0.8 -0.2 0.8 [4,] 1.2 0.8 -1.2 -1.2 -0.2 -0.2 [5,] 0.2 -0.2 0.8 0.8 -1.2 0.8 Regards. > Right Result Should Be: > > ans = > -0.8 -0.2 0.8 -1.2 0.8 -0.2 >0.2 -0.2 0.8 0.8 0.8 -1.2 > -0.8 -0.2 -1.2 0.8 -0.2 0.8 >1.2 0.8 -1.2 -1.2 -0.2 -0.2 >0.2 -0.2 0.8 0.8 -1.2 0.8 > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Counting occurrences of a set of values
df <- data.frame( V1= 1, V2= c( 2, 3, 2, 1), V3= c( 1, 2, 1, 1)) dfO <- df[ do.call( order, df), ] dfOD <- duplicated( dfO) dfODTrigger <- ! c( dfOD[-1], FALSE) dfOCounts <- diff( c( 0, which( dfODTrigger))) cbind( dfO[ dfODTrigger, ], dfOCounts) V1 V2 V3 dfOCounts 4 1 1 1 1 3 1 2 1 2 2 1 3 2 1 Regards On Thu, Sep 10, 2015 at 01:11:24PM +, Thomas Chesney wrote: > Can anyone suggest a way of counting how frequently sets of values occurs in > a data frame? Like table() only with sets. > > So for a dataset: > > V1, V2, V3 > 1, 2, 1 > 1, 3, 2 > 1, 2, 1 > 1, 1, 1 > > The output would be something like: > > 1,2,1: 2 > 1,3,2: 1 > 1,1,1: 1 > > Thank you, > > Thomas Chesney > > > > This message and any attachment are intended solely for the addressee > and may contain confidential information. If you have received this > message in error, please send it back to me, and immediately delete it. > > Please do not use, copy or disclose the information contained in this > message or in any attachment. Any views or opinions expressed by the > author of this email do not necessarily reflect the views of the > University of Nottingham. > > This message has been checked for viruses but the contents of an > attachment may still contain software viruses which could damage your > computer system, you are advised to perform your own checks. Email > communications with the University of Nottingham may be monitored as > permitted by UK legislation. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with vectors!
c( as.factor( VAS)) On Sat, Sep 05, 2015 at 02:14:18PM -0700, Dan D wrote: > # your data > VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow","Black","Green","Black") > > # declare the new vector > New_Vector<-numeric(length(VAS)) > > # brute force: > New_Vector[VAS=="White"]<-1 > New_Vector[VAS=="Yellow"]<-2 > New_Vector[VAS=="Green"]<-3 > New_Vector[VAS=="Black"]<-4 > > # a little more subtle > cols<-c("White","Yellow","Green","Black") > for (i in 1:length(cols)) New_Vector[VAS==cols[i]]<-i > > # and a general approach (that may give a different indexing, but can be > used for any array) > for (i in 1:length(unique(VAS))) New_Vector[VAS==unique(VAS)[i]]<-i > cbind(1:length(unique(VAS)),unique(VAS)) # a decoding key for the color > index > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Help-with-vectors-tp4711801p4711895.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with vectors!
Just for fun: > colSums( outer( VAS, VAS, '<')) [1] 3 3 0 3 7 8 8 0 3 0 On Sat, Sep 05, 2015 at 02:14:18PM -0700, Dan D wrote: > # your data > VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow","Black","Green","Black") > > # declare the new vector > New_Vector<-numeric(length(VAS)) > > # brute force: > New_Vector[VAS=="White"]<-1 > New_Vector[VAS=="Yellow"]<-2 > New_Vector[VAS=="Green"]<-3 > New_Vector[VAS=="Black"]<-4 > > # a little more subtle > cols<-c("White","Yellow","Green","Black") > for (i in 1:length(cols)) New_Vector[VAS==cols[i]]<-i > > # and a general approach (that may give a different indexing, but can be > used for any array) > for (i in 1:length(unique(VAS))) New_Vector[VAS==unique(VAS)[i]]<-i > cbind(1:length(unique(VAS)),unique(VAS)) # a decoding key for the color > index > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Help-with-vectors-tp4711801p4711895.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with vectors!
# my last one: xtfrm( VAS) On Tue, Sep 08, 2015 at 11:55:51AM -0700, Dan D wrote: > Great! > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Help-with-vectors-tp4711801p4712023.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] extracting every nth character from a string...
> rawToChar( charToRaw( str)[ c( TRUE, FALSE)]) [1] "ACEG" Regards On Sat, Sep 05, 2015 at 04:59:54PM -0400, Evan Cooch wrote: > Suppose I had the following string, which has length of integer multiple of > some value n. So, say n=2, and the example string has a length of (2x4) = 8 > characters. > > str <- "ABCDEFGH" > > What I'm trying to figure out is a simple, base-R coded way (which I > heuristically call StrSubset in the following) to extract every nth > character from the string, to generate a new string. > > So > > str <- "ABCDEFGH" > > new_str <- StrSubset(str); > > print(new_str) > > which would yield > > "ACEG" > > > Best I could come up with is something like the following, where I extract > every odd character from the string: > > StrSubset <- function(string) > { > paste(unlist(strsplit(string,""))[seq(1,nchar(string),2)],collapse="") } > > > Anything more elegant come to mind? Trying to avoid regex if possible > (harder to explain to end-users), but if that meets the 'more elegant' sniff > test, happy to consider... > > Thanks in advance... > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with vectors!
On Sat, Sep 05, 2015 at 02:14:18PM -0700, Dan D wrote: > # your data > VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow","Black","Green","Black") > > # declare the new vector > New_Vector<-numeric(length(VAS)) > > # brute force: > New_Vector[VAS=="White"]<-1 > New_Vector[VAS=="Yellow"]<-2 > New_Vector[VAS=="Green"]<-3 > New_Vector[VAS=="Black"]<-4 > > # a little more subtle > cols<-c("White","Yellow","Green","Black") > for (i in 1:length(cols)) New_Vector[VAS==cols[i]]<-i > > # and a general approach (that may give a different indexing, but can be > used for any array) > for (i in 1:length(unique(VAS))) New_Vector[VAS==unique(VAS)[i]]<-i > cbind(1:length(unique(VAS)),unique(VAS)) # a decoding key for the color > index > # how about: rank( VAS, ties.method='min') Regards __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] unlist( list( factor( 'a'), 1)) == c( 1, 1); unlist( list( factor( 'a'), factor( 1)))==c( 'a', '1')
Hi, c( factor( 'a'), ( 1)) [1] 1 1 c( factor( 'a'), factor( 1)) [1] 1 1 c( factor( 'a'), factor( 'b')) [1] 1 1 unlist( list( factor( 'a'), 1)) [1] 1 1 unlist( list( factor( 'a'), factor( 1))) [1] a 1 Levels: a 1 unlist( list( factor( 'a'), factor( 'b'))) [1] a b Levels: a b In an data.frame it is the same unlist( data.frame( factor( 'a'), factor( 1))) factor..a.. factor.1. a 1 Levels: a 1 unlist( data.frame( factor( 'a'), ( 1))) factor..a..X.1. 1 1 Im not sure, whether this behaviour can be seen as an error. But if I for instance use read.table or scan then these functions will use data.frame(..., # with stringsAsFactors = default.stringsAsFactors()) And so rows will by default appear, which contains factors and numbers. If i want to convert the content in an as.character datatype then the factor indexes will be used instead of the strings. This behaviour changes, if i switch off the global Option options( stringsAsFactors= FALSE) unlist( data.frame( 'a', ( 1))) X.a. X.1. unlist( list( 'a', ( 1))) [1] a 1 This leads to some questions: If factors represent an optimized vector of strings (or other datatypes) why can an operation on this factor not behave like an operation on vector of strings (or ...)? And: what is the best solution to convert an list( factor, number) to its character representation? Or: is it a bug? Regards __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] about lm()
Please provide some data from your variable data. Show the output of dput( data) or an subset of data which leads toe the specific error. Regards On Sun, Mar 30, 2014 at 02:23:09PM +0100, Si Qi L. wrote: Hi I have a problem with linear regression. This is my codes: acc1- lm(data$acc ~ dummy + data$Reqloanamount + data$Code + data$Code.1 + data$EmpNetMonthlyPay + data$Customer_Age + data$RTI) summary(acc1) These attributes are all numerical except the acc(factors), so how can I fix the problem R showed me? can anyone please help me out? Many thanks! But the R shows: Residuals:Error in quantile.default(resid) : factors are not allowedIn addition: Warning message:In Ops.factor(r, 2) : ^ not meaningful for factors [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help! Weird behavioral difference between R interactive and command-line?
Hi some functions, like plot, readline and others behave different depending to the result of the function interactive() there is no way to ask R which functions do that On Thu, Sep 06, 2012 at 01:22:32PM -0500, Michael wrote: Help! Weird behavioral difference between R interactive and command-line? Hi all, This weird problem has been bugging me for a while. If I run the R program in an interactive session, it worked; But if I run the R program on command-line, it stopped with no reason: with Execution halted message and without any specific error messages. What could be the problem? Thanks a lot! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Start R from bash/bat file and end in interactive mode
one way could be: $ cat rook.R | R --interactive but with a little different console behaviour the problem in your other examples is, that R uses only the first appearing switch of: --interactive, --f, -e, --vanilla, --no-save, ... the switches cannot be combined On Thu, Nov 01, 2012 at 09:14:09AM +0100, Jan van der Laan wrote: I have a r-script (rook.R) that starts a Rook server. To present users from having to start R and type in source(rook.R), I want to create a bash script and bat file that starts R and sources the script. However, to keep the Rook server running R should not close after running the script and stay in interactive mode. This proves more difficult than expected. I tried various combinations of commandline parameters and pipes, but none of them seem to work: $R -f rook.R --interactive Runs and quits $ cat rook.R | R Fatal error: you must specify '--save', '--no-save' or '--vanilla' $cat rook.R | R --no-save Runs and quits $R --no-save rook.R Runs and quits $R --no-save --interactive rook.R Runs and quits I would have expected the --interactive parameter to do what I want, but it doesn't seem to do anything. What does work is to create a .Rprofile with sourc(rook.R) in it in the directory and then start R (just plain R). However I don't find this a very elegant solution. I could of create the .Rprofile file in the bash script which is somewhat better, but still not very elegant. I end up with the following bash script: #!/bin/bash echo source(\rook.R\) .Rprofile R Another, not very elegant, possible solution which I haven't tried is to start a while loop at the end of the script with a sleep command in it. Does there exist a better solution? Regards, Jan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Start R from bash/bat file and end in interactive mode
another working Example: { echo print( 1); cat;} | R --interactive but do not end with ctrl-D, it overrides the save workspace question; use q() instead there is an additional echo where i don't know how to avoid them On Thu, Nov 01, 2012 at 09:14:09AM +0100, Jan van der Laan wrote: I have a r-script (rook.R) that starts a Rook server. To present users from having to start R and type in source(rook.R), I want to create a bash script and bat file that starts R and sources the script. However, to keep the Rook server running R should not close after running the script and stay in interactive mode. This proves more difficult than expected. I tried various combinations of commandline parameters and pipes, but none of them seem to work: $R -f rook.R --interactive Runs and quits $ cat rook.R | R Fatal error: you must specify '--save', '--no-save' or '--vanilla' $cat rook.R | R --no-save Runs and quits $R --no-save rook.R Runs and quits $R --no-save --interactive rook.R Runs and quits I would have expected the --interactive parameter to do what I want, but it doesn't seem to do anything. What does work is to create a .Rprofile with sourc(rook.R) in it in the directory and then start R (just plain R). However I don't find this a very elegant solution. I could of create the .Rprofile file in the bash script which is somewhat better, but still not very elegant. I end up with the following bash script: #!/bin/bash echo source(\rook.R\) .Rprofile R Another, not very elegant, possible solution which I haven't tried is to start a while loop at the end of the script with a sleep command in it. Does there exist a better solution? Regards, Jan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] suggestions argument in rbga function in genalg package
On Thu, Sep 22, 2011 at 06:46:23PM +, Joseph Boyer wrote: Would someone be so kind as to provide example code where they use the suggestions argument in the rgba function In genalg? I can't get it to work. The following code works just fine: GenFit -rbga(Lower, Upper, evalFunc = evaluate) Lower and Upper are each numeric vectors with 7 elements. Evaluate is an objective function. However, when I want to use a suggested chromosome, I get an error message. My code is start - c(1,0.1,10, 100,1,100,1) suggestions - list(start) GenFit -rbga(Lower, Upper, suggestions = suggestions, evalFunc = evaluate) The error message is: Error in 1:suggestionCount : argument of length 0 Thanks. rbga( c( -1, -1), c( 1, 1), evalFunc= function( string){ sum( string**2)**0.5}, suggestions=t( as.data.frame( c( 0, 0 Regards __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] possibly Error in R version 2.12.1 (2010-12-16)
How can i file this isssue as an bugreport? On Fri, Feb 03, 2012 at 02:01:02PM +0100, peter dalgaard wrote: On Feb 2, 2012, at 21:24 , Frank Schwidom wrote: Hi, the following Code demonstrates an possibly Error in R (or you can explain me, why this happens, thanks in advance) Looks like an effect of lazy evaluation: The value of i is not evaluated until after the loop has ended, at which point it will be 2. This is a feature, not an error, even if it confuses people at times... -pd Code: # testClass - function( stackData= c()) { list( write= function( ...) { sChain= for( s in c( stackData, ...)) { sChain= paste( sChain, '', sub( '', '', s), '', sep, sep='') } write( sChain, fHandle, append=TRUE) }, stackIt1 = function( ...) { testClass( stackData= c( stackData, ...)) }, stackIt2 = function( ...) { tmp= c( stackData, ...) testClass( stackData= tmp) }, getStack = function() { stackData }, NULL ) } to1= testClass() for( i in 4:2) { to1= to1$stackIt1( i) } print( all( rep( 2, 3) == to1$getStack())) # error! to2= testClass() for( i in 4:2) { to2= to2$stackIt2( i) } print( all( 4:2 == to2$getStack())) # correct! # what ist the difference between stackIt1 and stackIt2? # (error appears only by using an for loop) version _ platform i486-pc-linux-gnu arch i486 os linux-gnu system i486, linux-gnu status major 2 minor 12.1 year 2010 month 12 day16 svn rev53855 language R version.string R version 2.12.1 (2010-12-16) Regards # End of Code written in an R-File and called per source( 'Fname.R') shows 2 subsequent outputs of 'TRUE', which is not ok in my mind Thanks for your attention __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] possibly Error in R version 2.12.1 (2010-12-16)
Hi, the following Code demonstrates an possibly Error in R (or you can explain me, why this happens, thanks in advance) Code: # testClass - function( stackData= c()) { list( write= function( ...) { sChain= for( s in c( stackData, ...)) { sChain= paste( sChain, '', sub( '', '', s), '', sep, sep='') } write( sChain, fHandle, append=TRUE) }, stackIt1 = function( ...) { testClass( stackData= c( stackData, ...)) }, stackIt2 = function( ...) { tmp= c( stackData, ...) testClass( stackData= tmp) }, getStack = function() { stackData }, NULL ) } to1= testClass() for( i in 4:2) { to1= to1$stackIt1( i) } print( all( rep( 2, 3) == to1$getStack())) # error! to2= testClass() for( i in 4:2) { to2= to2$stackIt2( i) } print( all( 4:2 == to2$getStack())) # correct! # what ist the difference between stackIt1 and stackIt2? # (error appears only by using an for loop) version _ platform i486-pc-linux-gnu arch i486 os linux-gnu system i486, linux-gnu status major 2 minor 12.1 year 2010 month 12 day16 svn rev53855 language R version.string R version 2.12.1 (2010-12-16) Regards # End of Code written in an R-File and called per source( 'Fname.R') shows 2 subsequent outputs of 'TRUE', which is not ok in my mind Thanks for your attention __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to define new operator in R?
Hi, you may overwrite existing opreators in the current environment for example: `+` - `*` 2+3 [1] 6 Regards! On Wed, Mar 30, 2011 at 10:04:19AM -0600, Chuanlong Du wrote: Hello, everyone! Does anyone know how make some symbols have special means in R? For example, we know that + in R means the sum of the two operand on its left and right. I want to define some operators in R by myself. Is this possible? Regards! -- Chuanlong Du Department of Statistcis Iowa State University Ames, IA, US 50011 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.