Samuel Song Samuel Song - 1 year ago 78
R Question

Select a data frame column using $ and the name of the column as a string in a variable

I am going to rank a large dataset based on multiple different values for several times, and I would like to use a loop to do this for me. I have created a combination named

, in which I have listed all characteristic names in it.

parameter <- c("market_value_LOCAL", "ep", "book_price",
"sales_price", "dividend_yield","beta",

I was trying to use the following command to sort data by increasing order of selected parameter

Q1_R1000_parameter <- Q1_R1000[order(Q1_R1000$parameter[X]),]

(because I have 10 items in

Answer Source

You can't do that kind of subsetting with $. In the source code (R/src/main/subset.c) it states:

/*The $ subset operator.
We need to be sure to only evaluate the first argument.
The second will be a symbol that needs to be matched, not evaluated.

Second argument? What?! You have to realise that $, like everything else in R, (including for instance ( , + , ^ etc) is a function, that takes arguments and is evaluated. df$V1 could be rewritten as

`$`(df , V1)

or indeed

`$`(df , "V1")


`$`(df , paste0("V1") )

...for instance will never work, nor will anything else that must first be evaluated in the second argument. You may only pass a string which is never evaluated.

Instead use [ (or [[ if you want to extract only a single column as a vector).

For example,

var <- "mpg"
#Doesn't work
#These both work, but note that what they return is different
# the first is a vector, the second is a data.frame

You can perform the ordering without loops, using to construct the call to order. Here is a reproducible example below:

#  set seed for reproducibility
df <- data.frame( col1 = sample(5,10,repl=T) , col2 = sample(5,10,repl=T) , col3 = sample(5,10,repl=T) )

#  We want to sort by 'col3' then by 'col1'
sort_list <- c("col3","col1")

#  Use '' to call order. Seccond argument in is a list of arguments
#  to pass to the first argument, in this case 'order'.
#  Since  a data.frame is really a list, we just subset the data.frame
#  according to the columns we want to sort in, in that order
df[ order , df[ , match( sort_list , names(df) ) ]  ) , ]

   col1 col2 col3
10    3    5    1
9     3    2    2
7     3    2    3
8     5    1    3
6     1    5    4
3     3    4    4
2     4    3    4
5     5    1    4
1     2    5    5
4     5    3    5
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download