[R] access elements of a named list using a factor

Robin Hankin rksh1 at cam.ac.uk
Fri Oct 23 15:31:37 CEST 2009


Hi

I have a factor 'f' and a named list 'jj'.

I want names(jj) to match up with levels(f).

How do I use levels(f) to access elements of jj?


 > f <- factor(c("pigs","pigs","slugs"))
 > f
[1] pigs  pigs  slugs
Levels: pigs slugs
 >
 > jj <- list(pigs=1:10,slugs=1:3)



My attempts to produce jj$pigs all give errors:

 > jj$levels(f)[1]
Error: attempt to apply non-function

 > do.call("$",jj,levels(f)[1])
Error in if (quote) { : argument is not interpretable as logical

 >  "$"(jj,levels(f)[1])
Error in jj$levels(f)[1] : invalid subscript type 'language'






-- 
Robin K. S. Hankin
Uncertainty Analyst
University of Cambridge
19 Silver Street
Cambridge CB3 9EP
01223-764877




More information about the R-help mailing list