Odp: Using a string as a variable name - revisited
Hi Without some insight about foo, list or counts it is impossible to say what is wrong. mat<-matrix(1:12, 3,4) colnames(mat)<-letters[1:4] DF<-as.data.frame(mat) fac<-factor(names(DF))
fac
[1] a b c d Levels: a b c d
ff<-fac[3] ff
[1] c Levels: a b c d
DF[[ff]]
[1] 7 8 9
ff<-fac[1] DF[[ff]]
[1] 1 2 3 As you can see with DF as data frame and ff extracted from vector of name as a factor everything seems to work OK. When anybody except you wont to do foo <- list$taxon[match(5,list$item)] he gets Error in list$taxon : object of type 'builtin' is not subsettable So provide some toy example or at least structure of objects you use and you probably get solution. Regards Petr
r-help-bounces at r-project.org napsal dne 01.04.2010 23:24:55:
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 -- Jim Holtman Cincinnati, OH +1 513 646 9390 ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.