Re: [R] access elements of a named list using a factor
Robin, see below my inserted comments. Robin Hankin wrote: Hello Dimitris thanks for this. It works! I guess I was fixated on the dollar sign. I must confess that I don't really understand any of the error messages below. Can anyone help me interpret them? rksh Dimitris Rizopoulos wrote: do you mean: f <- factor(c("pigs", "pigs", "slugs")) jj <- list(pigs = 1:10, slugs = 1:3) jj[levels(f)[1]] jj[[levels(f)[1]]] Best, Dimitris Robin Hankin wrote: 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 jj$levels(f): R's guess is that you want to apply some function jj$levels to f. Since jj$levels is NULL, it is not a function and R responds with the messages of the "attempt to apply a non-function". > do.call("$",jj,levels(f)[1]) Error in if (quote) { : argument is not interpretable as logical That's right: ?do.call tells us about the usage: do.call(what, args, quote = FALSE, envir = parent.frame()) hence you asked to apply the function what = "$" on the arguments given in list args = jj (i.e. first arg is first element of jj and second arg is second element of jj) and you wanted to use quote = levels(f)[1]. If you had rather used teh ugly do.call("$", list(jj,levels(f)[1])) it should have work (all argumentes in a list). Dimitris already gave the answer how to do it much better. > "$"(jj,levels(f)[1]) Error in jj$levels(f)[1] : invalid subscript type 'language' Here the call is constructed using levels(f)[1] rather than its value again. Best wishes, Uwe __ R-help@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.
Re: [R] access elements of a named list using a factor
Hello Dimitris thanks for this. It works! I guess I was fixated on the dollar sign. I must confess that I don't really understand any of the error messages below. Can anyone help me interpret them? rksh Dimitris Rizopoulos wrote: do you mean: f <- factor(c("pigs", "pigs", "slugs")) jj <- list(pigs = 1:10, slugs = 1:3) jj[levels(f)[1]] jj[[levels(f)[1]]] Best, Dimitris Robin Hankin wrote: 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 __ R-help@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.
Re: [R] access elements of a named list using a factor
do you mean: f <- factor(c("pigs", "pigs", "slugs")) jj <- list(pigs = 1:10, slugs = 1:3) jj[levels(f)[1]] jj[[levels(f)[1]]] Best, Dimitris Robin Hankin wrote: 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' -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ R-help@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.
[R] access elements of a named list using a factor
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 __ R-help@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.