[R] Using a string as a variable name - revisited
Erik Iverson
eriki at ccbr.umn.edu
Thu Apr 1 23:32:03 CEST 2010
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Euan Reavie wrote:
> I would like to revisit a problem that was discussed previously (see
> quoted discussion below). I am trying to do the same thing, using a
> string to indicate a column with the same name. I am making "foo" a
> string taken from a list of names. It matches the row where "item" =
> 5, and picks the corresponding "taxon"
>
>> foo <- list$taxon[match(5,list$item)]
>
> Let's say this returns foo as "Aulacoseira_islandica". I have another
> matrix "counts" with column headers corresponding to the taxon list.
> But, when I try to access the data in the Aulacoseira_islandica
> column, it instead uses the data from another column. For instance...
>
>> columndata <- counts[[foo]]
>
> ...returns the data from the wrong column. What it seems to be doing
> is converting the text "Aulacoseira_islandica" to a number (25, for
> some reason) and reading the count data from column number 25, instead
> of from the column labelled with Aulacoseira_islandica.
>
> If I try...
>
>> columndata <- counts$Aulacoseira_islandica
>
> ...it works fine. Any thoughts?
>
> -Euan
> NRRI-University of Minnesota Duluth
>
>
> ______________________
> Jason Horn-2
> Oct 20, 2006; 06:28pm
> [R] Using a string as a variable name
>
> Is it possible to use a string as a variable name? For example:
>
> foo<="var1"
> frame$foo # frame is a data frame with with a column titled "var1"
>
> This does not work, unfortunately. Am I just missing the correct
> syntax to make this work?
>
> - Jason
> ______________________________________________
> Oct 20, 2006; 06:30pm
> Re: [R] Using a string as a variable name
>
> frame[[foo]]
>
> On 10/20/06, Jason Horn <[hidden email]> wrote:
>
>> Is it possible to use a string as a variable name? For example:
>>
>> foo<="var1"
>> frame$foo # frame is a data frame with with a column titled "var1"
>>
>> This does not work, unfortunately. Am I just missing the correct
>> syntax to make this work?
>>
>>
>> - Jason
>
More information about the R-help
mailing list