使用$和列名称向量动态选择数据框架列 [英] Dynamically select data frame columns using $ and a vector of column names
问题描述
我希望根据不同的列排序一个数据框,一个轮到一个。我有一个字符向量,其中包含订单
应该基于的相关列名:
I wish to order a data frame based on different columns, one at a turn. I have a character vector with the relevant column names on which the order
should be based:
parameter <- c("market_value_LOCAL", "ep", "book_price", "sales_price", "dividend_yield",
"beta", "TOTAL_RATING_SCORE", "ENVIRONMENT", "SOCIAL", "GOVERNANCE")
我想循环参数中的名称,并动态选择列用于订单
我的数据:
I wish to loop over the names in "parameter" and dynamically select the column to be used to order
my data:
Q1_R1000_parameter <- Q1_R1000[order(Q1_R1000$parameter[X]), ]
其中 X
是 1:10
(因为我在参数中有10个项目)。
where X
is 1:10
(because I have 10 items in "parameter").
为了使我的示例可重现,请考虑数据集mtcars和存储在字符向量cols中的一些变量名。当我尝试使用动态子集cols从mtcars中选择一个变量时,以与上述相似的方式( Q1_R1000 $ parameter [X]
),列未选择:
To make my example reproducible, consider the data set "mtcars" and some variable names stored in a character vector "cols". When I try to select a variable from "mtcars" using a dynamic subset of "cols", in a similar way as above (Q1_R1000$parameter[X]
), the column is not selected:
cols <- c("cyl", "am")
mtcars$cols[1]
# NULL
推荐答案
用 $
做这种子集。在源代码( R / src / main / subset.c
)中,它指出:
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.
*/
第二个参数?什么?!你必须意识到 $
像R中的其他内容一样(包括例如(
, +
, ^
等)是一个函数,它接受参数并进行评估。 df $ V1
可以重写为
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)
或确实
`$`(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.
而是使用 [
(或 [[
if您只需要提取一个列作为向量)。
Instead use [
(or [[
if you want to extract only a single column as a vector).
例如,
var <- "mpg"
#Doesn't work
mtcars$var
#These both work, but note that what they return is different
# the first is a vector, the second is a data.frame
mtcars[[var]]
mtcars[var]
您可以使用 do.call
执行无循环的排序来构造对订单的调用
。以下是可重复的示例:
You can perform the ordering without loops, using do.call
to construct the call to order
. Here is a reproducible example below:
# set seed for reproducibility
set.seed(123)
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 'do.call' to call order. Seccond argument in do.call 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[ do.call( 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
这篇关于使用$和列名称向量动态选择数据框架列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!