I wonder if there exists some kind of inverse of the "names" primitive in R. Let me explain what do I mean: If I create a list: -> li <- list(a=1, b=2, c=3, d=4) then I can have: -> names(li) [1] "a" "b" "c" "d" which is, I guess, some kind of vector, since -> typeof(names(li)) [1] "character" however, I haven't seen something that allows me to get the other side, i.e., the values. Something like: ->VALUES(li) [1] 1 2 3 4 Do you have any comments on this? Thanks, - Sergio. [[alternative HTML version deleted]]
Is this what you want?:> li <- list(a=1, b=2, c=3, d=4) > li$a [1] 1 $b [1] 2 $c [1] 3 $d [1] 4> unlist(li)a b c d 1 2 3 4> unname(unlist(li))[1] 1 2 3 4 Regards S?ren -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Julio Sergio Santana Sent: 21. august 2012 00:20 To: r-help at r-project.org Subject: [R] Some kind of inverse of "names" I wonder if there exists some kind of inverse of the "names" primitive in R. Let me explain what do I mean: If I create a list: -> li <- list(a=1, b=2, c=3, d=4) then I can have: -> names(li) [1] "a" "b" "c" "d" which is, I guess, some kind of vector, since -> typeof(names(li)) [1] "character" however, I haven't seen something that allows me to get the other side, i.e., the values. Something like: ->VALUES(li) [1] 1 2 3 4 Do you have any comments on this? Thanks, - Sergio. [[alternative HTML version deleted]] ______________________________________________ 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.
Inline -- Bert On Mon, Aug 20, 2012 at 3:19 PM, Julio Sergio Santana <juliosergio at gmail.com> wrote:> I wonder if there exists some kind of inverse of the "names" primitive in > R. Let me explain what do I mean: > > If I create a list: > -> li <- list(a=1, b=2, c=3, d=4) > then I can have: > -> names(li) > [1] "a" "b" "c" "d" > which is, I guess, some kind of vector, since > -> typeof(names(li)) > [1] "character" > however, I haven't seen something that allows me to get the other side, > i.e., the values. > Something like: > ->VALUES(li) > [1] 1 2 3 4 > > Do you have any comments on this?Yes. Read "An Introduction to R" . You do not understand lists. Also see ?unlist -- Bert> > > Thanks, > - Sergio. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Hi Sergio, 'names' are just an attribute of your list. If all elements of your list are of the same type (i.e. integer in your example) you may try something like 'unlist (li)' or even 'as.numeric(unlist(li))'. This will give you the values you wanted. An other approach is organizig your data not as a list but as a named vector by simply using concatenate: li <- c(a=1, b=2, c=3, d=4) This will create a named integer vector (at least I think so) and you will receive the data by calling 'li'. Hope this helped you a little. Peter Am 21.08.2012 00:19, schrieb Julio Sergio Santana:> I wonder if there exists some kind of inverse of the "names" primitive in > R. Let me explain what do I mean: > > If I create a list: > -> li <- list(a=1, b=2, c=3, d=4) > > then I can have: > -> names(li) > [1] "a" "b" "c" "d" > which is, I guess, some kind of vector, since > -> typeof(names(li)) > [1] "character" > however, I haven't seen something that allows me to get the other side, > i.e., the values. > Something like: > ->VALUES(li) > [1] 1 2 3 4 > > Do you have any comments on this? > > > Thanks, > - Sergio. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >-- Institute of Earth and Environmental Sciences, University of Potsdam Karl-Liebknecht-Str. 24/25 (House 12), 14476 Potsdam, Germany office: +49 331 977 2469 mobile: +49 173 3732867 e-mail: peter.vorpahl at uni-potsdam.de
S?ren H?jsgaard <sorenh <at> math.aau.dk> writes:> > Is this what you want?: >Yes, that is exactly what I wanted! Thanks, --Sergio.
Hi Sergio, 'names' are just an attribute of your list. If all elements of your list are of the same type (i.e. integer in your example) you may try something like 'unlist (li)' or even 'as.numeric(unlist(li))'. This will give you the values you wanted. An other approach is organizig your data not as a list but as a named vector by simply using concatenate: li <- c(a=1, b=2, c=3, d=4) This will create a named integer vector (at least I think so) and you will receive the data by calling 'li'. Hope this helped you a little. Peter Am 21.08.2012 00:19, schrieb Julio Sergio Santana:> I wonder if there exists some kind of inverse of the "names" primitive in > R. Let me explain what do I mean: > > If I create a list: > -> li <- list(a=1, b=2, c=3, d=4) > then I can have: > -> names(li) > [1] "a" "b" "c" "d" > which is, I guess, some kind of vector, since > -> typeof(names(li)) > [1] "character" > however, I haven't seen something that allows me to get the other side, > i.e., the values. > Something like: > ->VALUES(li) > [1] 1 2 3 4 > > Do you have any comments on this? > > > Thanks, > - Sergio. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >