[R] passing variables into R

arun smartpink111 at yahoo.com
Tue Mar 18 18:17:56 CET 2014


#if you want to just extract the columns of dat1 using vector1, you can just use:
 dat1[,vector1]
A.K.






On Tuesday, March 18, 2014 1:13 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Not sure about your expected output.  May be this helps:
vector1 <- c("LA_A", "LA_B", "G_A")
dat1 <- setNames(as.data.frame(matrix(1:20,ncol=4)),c(vector1,"G_Z"))
lapply(vector1, function(i) with(dat1,get(i)))
#or

for(i in vector1){
 print(with(dat1,get(i)))}


A.K.


Hello, 

I just started learning R, and have the following problem. 
Would like to get the specific columns one by one, but do not know how to pass variables in R 

A piece of code below (does not work :) ) 


vector <- c("LA_A", "LA_B", "G_A",    "G_B",    "CK_A",    "CK_B",    "LDH_A",    "LDH_B",    "AST_A",    "AST_B",    "CRP_A",    "CRP_B", 
           "ALT_A",    "ALT_B",    "CT_A",    "CT_B",    "BUN_A",    "BUN_B",    "C_A",    "C_B",    "T_A",    "T_B") 

for ( i in vector ) { 
  i <- as.name(i) 
  dataframe$i 
}



More information about the R-help mailing list