使用$和字符值动态选择数据框列 [英] Dynamically select data frame columns using $ and a character value

查看:68
本文介绍了使用$和字符值动态选择数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有不同列名的向量,并且我希望能够遍历每个列名,以便从data.frame中提取该列.例如,考虑数据集mtcars和一些存储在字符向量cols中的变量名.当我尝试使用cols的动态子集从mtcars中选择一个变量时,这些工作还是会

I have a vector of different column names and I want to be able to loop over each of them to extract that column from a data.frame. For example, 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, nether of these work

cols <- c("mpg", "cyl", "am")
col <- cols[1]
col
# [1] "mpg"

mtcars$col
# NULL
mtcars$cols[1]
# NULL

如何获取这些值以返回与

how can I get these to return the same values as

mtcars$mpg

此外,我如何遍历cols中的所有列以某种形式获取值.

Furthermore how can I loop over all the columns in cols to get the values in some sort of loop.

for(x in seq_along(cols)) {
   value <- mtcars[ order(mtcars$cols[x]), ]
}

推荐答案

您不能使用$进行这种子设置.在源代码(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.

请改为使用[(如果要仅将单个列提取为向量,则请使用[[).

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构造对order的调用,您可以执行不带循环的排序.这是下面的可重现示例:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆