[R] list subselect by name ?

David Winsemius dwinsemius at comcast.net
Sat Sep 9 21:20:33 CEST 2017


> On Sep 9, 2017, at 11:45 AM, Andre Mikulec <andre_mikulec at hotmail.com> wrote:
> 
> list subselect by name ?
> -------------------------
> 
> I have this 'list of two elements of named elements.'
> 
>> list(letters=letters, LETTERS=LETTERS)[c("letters","LETTERS")]
> 
> $letters
> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
> [20] "t" "u" "v" "w" "x" "y" "z"
> 
> $LETTERS
> [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
> [20] "T" "U" "V" "W" "X" "Y" "Z"
> 
>> str( list(letters=letters, LETTERS=LETTERS)[c("letters","LETTERS")] )
> List of 2
> $ letters: chr [1:26] "a" "b" "c" "d" ...
> $ LETTERS: chr [1:26] "A" "B" "C" "D" ...
> 
> What code do I need to write in place of  ??????????????????
> to return a 'list of one element that is named.'
> 
>> list(letters=letters, LETTERS=LETTERS)????????????????????
> 
> $LETTERS
> [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
> [20] "T" "U" "V" "W" "X" "Y" "Z"
> 
>> str( list(letters=letters, LETTERS=LETTERS)???????????????????? )
> 
> List of 1
> $ LETTERS: chr [1:26] "A" "B" "C" "D" ...

Wouldn't this just be one of these:

#1
list(letters=letters, LETTERS=LETTERS)["LETTERS"]
#2
list(letters=letters, LETTERS=LETTERS)[2]
#3
mylist <- list(letters=letters, LETTERS=LETTERS)
mylist[ which(names(mylist) == "LETTERS") ]

-- 
David.


> 
> Thanks,
> 
> Andre Mikulec
> Andre_Mikulec at Hotmail.com
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law



More information about the R-help mailing list