简体   繁体   English

带有 R 中列列表的子集数据框

[英]Subset dataframe with list of columns in R

I want to select all columns in my dataframe which I have stored in a string variable.我想选择存储在字符串变量中的数据框中的所有列。 For example:例如:

v1 <- rnorm(100)
v2 <- rnorm(100)
v3 <- rnorm(100)
df <- data.frame(v1,v2,v3)

I want to accomplish the following:我想完成以下任务:

df[,c('v1','v2')]

But I want to use a variable instead of (c('v1', 'v2'))(these all fail):但我想使用一个变量而不是 (c('v1', 'v2'))(这些都失败了):

select.me <- "'v1','v2'"
df[,select.me]
df[,c(select.me)]
df[,c(paste(select.me,sep=''))]

Thanks for help with a simple question,感谢您帮助解决一个简单的问题,

The great irony here is that when you said "I want to do this" the first expression should have succeeded, 这里的讽刺是,当你说“我想这样做”时,第一个表达应该成功,

df[,c('v1','v2')]
> str( df[,c('v1','v2')] )
'data.frame':   100 obs. of  2 variables:
 $ v1: num  -0.3347 0.2113 0.9775 -0.0151 -1.8544 ...
 $ v2: num  -1.396 -0.95 -1.254 0.822 0.141 ...

whereas all the later attempts would fail. 而所有后来的尝试都会失败。 I later realized that you didn't know that you could use select.me <- c('v1','v2') ; df[ , select.me] 我后来意识到你不知道你可以使用select.me <- c('v1','v2') ; df[ , select.me] select.me <- c('v1','v2') ; df[ , select.me] . select.me <- c('v1','v2') ; df[ , select.me] You could also use these forms which might be safer in some instances: 您也可以使用这些在某些情况下可能更安全的表单:

df[ , names(df) %in% select.me] # logical indexing
df[ , grep(select.me, names(df) ) ]  # numeric indexing
df[ , grepl(select.me, names(df) ) ]  # logical indexing

Any of those can be used with negation( !logical ) or minus ( -numeric ) to retrieve the complement, whereas you cannot use character indexing with negation. 其中任何一个都可以用于否定( !logical )或减号( -numeric )来检索补码,而不能使用带有否定的字符索引。 If you wanted to go down one level in understandability and were willing to change the select.me values to a valid R expression you could do this: 如果您希望在可理解性方面达到一个级别,并且愿意将select.me值更改为有效的R表达式,则可以执行以下操作:

select.me <- "c('v1','v2')"
df[ , eval(parse(text=select.me)) ]

Not that I recommend this... just to let you know that such is possible after you "learn to walk". 不是我推荐这个...只是为了让你知道在你“学会走路”之后这种可能性。 It would also have been possible (although rather baroque) using your original quoted string to pull out the information (although I think this just illustrates why your first version is superior): 也可能(虽然相当巴洛克式)使用您的原始引用字符串来提取信息(尽管我认为这只能说明为什么您的第一个版本更优秀):

select.me <- "'v1','v2'"
df [ , scan(textConnection(select.me), what="", sep=",") ]
> str( df [ , scan(textConnection(select.me), what="", sep=",") ] )
Read 2 items
'data.frame':   100 obs. of  2 variables:
 $ v1: num  -0.3347 0.2113 0.9775 -0.0151 -1.8544 ...
 $ v2: num  -1.396 -0.95 -1.254 0.822 0.141 ...

This is basic R sytnax, perhaps you need to read the introductory manual 这是基本的R sytnax,也许您需要阅读介绍性手册

select.me <- c('v1','v2')
df[,select.me]

你是说这个吗?

dat <- cbind(df$v1, df$v2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM