A subset of a dataframe with a list of columns in R

I want to highlight all the columns in my data framework that I saved in a string variable. For instance:

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

I want to do the following:

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

But I want to use a variable instead of (c ('v1', 'v2')) (they all do not work):

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

Thanks for the help with a simple question,

+4
source share
3 answers

This is the basic R sytnax, maybe you need to read the introductory guide

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

The great irony is that when you said, β€œI want to do this,” the first expression must be successful,

 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 ... 

while all subsequent attempts fail. Later I realized that you did not know that you could use select.me <- c('v1','v2') ; df[ , select.me] select.me <- c('v1','v2') ; df[ , select.me] . You can also use these forms, which may be more secure in some cases:

 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 these can be used with negation ( !logical ) or minus ( -numeric ) to extract the complement, while you cannot use negative character indexing. If you want to go down one level in intelligibility and are ready to change the select.me values ​​to a valid R expression, you can do this:

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

Not that I recommend it ... just to let you know that this is possible after you "learn to walk." It would also be possible (albeit rather baroque) to use your source 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 ... 
+16
source

Do you mean this?

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

All Articles