R如何使用字符串变量来选择使用$符号的数据框架列 [英] R how use a string variable to select a data frame column using $ notation

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

问题描述

从我从前读过的R中,我可以通过以下两种方法之一来选择数据框中的列:frame [,column]或frame $ column。但是,当我有一个字符串作为变量时,它只能在第一个。换句话说,请考虑以下内容:



我有一个数据帧,tmp是问题响应的较大数据帧的子集。 V1是应答者的ID,Q5.3是响应,1或0:

  V1 Q5.3 
2 R_bdyKkzWcvBxDFTT 1
3 R_41wnKUQcM8mUW2x 0
4 R_2ogeykkgbH2e4RL 1
5 R_8D4jzMBfYO0M0ux 1
6 R_3KPgP2pxWROnip7 1

str(tmp)

'data.frame':5 obs。的2个变量:
$ V1:因子w / 364级别R_0039orNoOoWaDQx,..:256 116 70 201 95
$ Q5.3:num 1 0 1 1 1

现在,我定义一个变量x,它包含一列的名称的字符串。

  x<  - Q5.3

tmp [,x]返回我认为应该返回的内容:

  tmp [,x] 

[1] 1 0 1 1 1

tmp $Q5.3返回什么我认为应该返回:

  tmp $Q5.3

[1] 1 0 1 1 1

tmp $ x然后返回

  tmp $ x 

NULL

如何告诉R将tmp $ x解释为tmp $Q5.3



谢谢!

解决方案

如果您有 tmp 中的列名称的变量 x tmp [,x] tmp [[x]] 是正确的提取方式它。您不能让R使用 tmp $ x 作为 tmp $Q5.3 tmp $ x 将始终引用tmp中名为x的项目。


From the reading I've been doing with R, I can select a column in a data frame by either of these two methods: frame[,column] or frame$column. However, when I have a string as a variable, it works only in the first. In other words, consider the following:

I have a data frame, tmp, a subset of a larger data frame of question responses. V1 is the responder's id, Q5.3 is the response, a 1 or 0:

            V1 Q5.3
2 R_bdyKkzWcvBxDFTT    1
3 R_41wnKUQcM8mUW2x    0
4 R_2ogeykkgbH2e4RL    1
5 R_8D4jzMBfYO0M0ux    1
6 R_3KPgP2pxWROnip7    1

str(tmp)

'data.frame':   5 obs. of  2 variables:
     $ V1  : Factor w/ 364 levels "R_0039orNoOoWaDQx",..: 256 116 70 201 95
     $ Q5.3: num  1 0 1 1 1

Now, I define a variable x, that holds the string of the name of one of the columns.

x<-"Q5.3"

tmp[,x] returns what I think it should return:

tmp[,x]

[1] 1 0 1 1 1

tmp$"Q5.3" returns what I think it should return:

tmp$"Q5.3"

[1] 1 0 1 1 1

tmp$x however returns

tmp$x

NULL

How can I tell R to interpret tmp$x as tmp$"Q5.3"

Thanks!

解决方案

If you have a variable x with a column name in tmp, tmp[,x] or tmp[[x]] are the correct way to extract it. You cannot get R to use treat tmp$x as tmp$"Q5.3". tmp$x will always refer to the item named "x" in "tmp".

这篇关于R如何使用字符串变量来选择使用$符号的数据框架列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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