[R] coercing a string naming to a variable name and return value

Roger D. Peng rpeng at jhsph.edu
Wed Sep 1 17:50:29 CEST 2004

You can use "[[", I think, as in


or, eqivalently for a data frame

basin.param[, string.variable]


Dewez Thomas wrote:
> Hi all,
> I haven't been able to find how to assess a variable who's name is
> constructed with paste. The problem is following: In a data frame, there are
> 12 columns whose title vary only by a digit, all other part being equal. I
> would like to compute a operation on a subset these variables and parse them
> in turn.
> the data frame "basin.param" contains columns called ratio1, ratio2,
> ratio3...
> in each of these ratios I want to retain only values above 0. This could be
> put like this
> crit<- which(basin.param$ratio1 > 0)
> basin.param$ratio1[crit] + basin.param$val[crit]
> OK, so what if I want to increment from ratio1 to ratio3 automatically and
> return the value of the variable represented by the string? (does this make
> sense?)
> # Create the variable name 
> for (i in 1:3){
> string.variable <- paste("basin.param$ratio", index, sep="")
> # What is the instruction to interpret the string.variable value ???
> }
> Cheers
> Thomas
> ***
> Le contenu de cet e-mail et de ses pièces jointes est destin...{{dropped}}
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list