mirek wyczesany
2010-Jul-26 18:54 UTC
[R] using string variable as order() function argument
Hello, In my script I would like to use a loop, which sorts the dataframe according to different columns, pointed by the string variable. id col1 col2 col3 1 10 0 4 8 2 11 1 2 2 3 12 0 8 3 4 13 0 5 5 Usually the order() function can be used like this: sorted = mytable**[order(column3) , ] which results in properly sorted table: ** id col1 col2 col3 2 11 1 2 2 3 12 0 8 3 4 13 0 5 5 1 10 0 4 8 **But when trying to use a string variable instead of "column3" name: columnname = "column3" **sorted = mytable**[order(columnname) , ]** this command is not properly evaluated and the effect is somewhat strange. Would you suggest some solution? Thanks a lot! Mirek
Hello, In my script I would like to use a loop, which sorts the dataframe according to different columns, pointed by the string variable. id col1 col2 col3 1 10 0 4 8 2 11 1 2 2 3 12 0 8 3 4 13 0 5 5 Usually the order() function can be used like this: sorted = mytable[order(column3) , ] which results in properly sorted table: id col1 col2 col3 2 11 1 2 2 3 12 0 8 3 4 13 0 5 5 1 10 0 4 8 But when trying to use a string variable instead of "column3" name: columnname = "column3" sorted = mytable[order(columnname) , ] this command is not properly evaluated and the effect is somewhat strange. Would you suggest some solution? Thanks a lot! Mirek
Duncan Murdoch
2010-Jul-26 19:01 UTC
[R] using string variable as order() function argument
On 26/07/2010 2:56 PM, mirek wrote:> Hello, > > In my script I would like to use a loop, which sorts the dataframe > according to different columns, pointed by the string variable. > > id col1 col2 col3 > 1 10 0 4 8 > 2 11 1 2 2 > 3 12 0 8 3 > 4 13 0 5 5 > > Usually the order() function can be used like this: > > sorted = mytable[order(column3) , ] > > which results in properly sorted table: > > id col1 col2 col3 > 2 11 1 2 2 > 3 12 0 8 3 > 4 13 0 5 5 > 1 10 0 4 8 > > But when trying to use a string variable instead of "column3" name: > > columnname = "column3" > sorted = mytable[order(columnname) , ] > > this command is not properly evaluated and the effect is somewhat strange.The argument to order() should be a vector whose sort order is to be returned. So you just need to extract one column from the dataframe, e.g. column <- mytable[, columnname] sorted <- mytable[order(column) , ] Duncan Murdoch
Hi, Is this what you want? ############## mytable <- read.table(textConnection(" id col1 col2 col3 10 0 4 8 11 1 2 2 12 0 8 3 13 0 5 5 "), header = TRUE) mytable columnname <- "col3" mytable[order(mytable[, columnname]), ] ################### Josh On Mon, Jul 26, 2010 at 11:54 AM, mirek wyczesany <miroslaw.wyczesany at uj.edu.pl> wrote:> Hello, > > In my script I would like to use a loop, which sorts the dataframe according > to different columns, pointed by the string variable. > > ? ?id col1 ?col2 ?col3 > 1 ? 10 ? ?0 ? ?4 ? ? ?8 > 2 ? 11 ? ?1 ? ?2 ? ? ?2 > 3 ? 12 ? ?0 ? ?8 ? ? ?3 > 4 ? 13 ? ?0 ? ?5 ? ? ?5 > > Usually the order() function can be used like this: > > sorted = mytable**[order(column3) , ] > > which results in properly sorted table: > ** > > ? ?id col1 ?col2 ?col3 > 2 ? 11 ? ?1 ? ?2 ? ? ?2 > 3 ? 12 ? ?0 ? ?8 ? ? ?3 > 4 ? 13 ? ?0 ? ?5 ? ? ?5 > 1 ? 10 ? ?0 ? ?4 ? ? ?8 > > **But when trying to use a string variable instead of "column3" name: > > columnname = "column3" > **sorted = mytable**[order(columnname) , ]** > > this command is not properly evaluated and the effect is somewhat strange. > > Would you suggest some solution? > > Thanks a lot! > > Mirek > > ______________________________________________ > R-help at 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Try this:> mytableid col1 col2 col3 1 10 0 4 8 2 11 1 2 2 3 12 0 8 3 4 13 0 5 5> colName <- 'col3' > mytable[order(mytable[[colName]]),]id col1 col2 col3 2 11 1 2 2 3 12 0 8 3 4 13 0 5 5 1 10 0 4 8> colName <- 'id' > mytable[order(mytable[[colName]]),]id col1 col2 col3 1 10 0 4 8 2 11 1 2 2 3 12 0 8 3 4 13 0 5 5>On Mon, Jul 26, 2010 at 2:54 PM, mirek wyczesany <miroslaw.wyczesany at uj.edu.pl> wrote:> Hello, > > In my script I would like to use a loop, which sorts the dataframe according > to different columns, pointed by the string variable. > > ? ?id col1 ?col2 ?col3 > 1 ? 10 ? ?0 ? ?4 ? ? ?8 > 2 ? 11 ? ?1 ? ?2 ? ? ?2 > 3 ? 12 ? ?0 ? ?8 ? ? ?3 > 4 ? 13 ? ?0 ? ?5 ? ? ?5 > > Usually the order() function can be used like this: > > sorted = mytable**[order(column3) , ] > > which results in properly sorted table: > ** > > ? ?id col1 ?col2 ?col3 > 2 ? 11 ? ?1 ? ?2 ? ? ?2 > 3 ? 12 ? ?0 ? ?8 ? ? ?3 > 4 ? 13 ? ?0 ? ?5 ? ? ?5 > 1 ? 10 ? ?0 ? ?4 ? ? ?8 > > **But when trying to use a string variable instead of "column3" name: > > columnname = "column3" > **sorted = mytable**[order(columnname) , ]** > > this command is not properly evaluated and the effect is somewhat strange. > > Would you suggest some solution? > > Thanks a lot! > > Mirek > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?