[R] Runtime column name creation

pvshankar info.shankar at gmail.com
Thu May 3 18:35:26 CEST 2012


Hello all, 

I have a data frame with column names s1, s2, s3....s11 

I have a function that gets two parameters, one is used as a subscript for
the column names  and another is used as an index into the chosen column. 

For example: 

my_func <- function(subscr, index) 
{ 
  if (subscr == 1) 
  { 
    df$s1[index] <- some value 
  } 
} 

The problem is, I do not want to create a bunch of if statements (one for
each 1:11 column names)). 
Instead, I want to "create" the column name in run time based on subscr
value. 

I tried eval(as.name(paste("df$s",subscr,sep="")))[index] <- some value 

and it complains that object df$s1 is not found. 

Could someone please help me with this? 
(Needless to say, I have just started programing in R) 

Thanks, 
Shankar

--
View this message in context: http://r.789695.n4.nabble.com/Runtime-column-name-creation-tp4606497.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list