"Robin Gruna" <[EMAIL PROTECTED]> writes:

> I have the following problem: I have a list as follows,
> 
> > values <- list(red = 1, yellow = 2, blue = 3)
> 
> > values
> $red
> [1] 1
> 
> $yellow
> [1] 2
> 
> $blue
> [1] 3
> 
> There is also a vector containing the diffrent "colors" as character strings:
> 
> > colors <- c("red", "red", "blue", "yellow", "red", "blue")
> > colors
> [1] "red"    "red"    "blue"   "yellow" "red"    "blue"  
> 
> Now i can attach the list values to R:
> 
> > attach(values)
> > red
> [1] 1
> 
> etc...
> 
> Now to my problem: How can I make R in a simple way to evaluate the strings "red", 
> "blue" etc. as variables, returning their numeric values ?
> As result I want to get a vector containing the values of the colors like this one:
> 
> > values.colors
> [1] 1 1 3 2 1 3

Forget about the attach() business, and do

values <- unlist(values) # or values <- c(red = 1, yellow = 2, blue = 3)
values.colors <- values[colors]

If you insist on going via variables, try

sapply(colors, get)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - ([EMAIL PROTECTED])             FAX: (+45) 35327907

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to