[R] a question on list manipulation

Duncan Murdoch murdoch.duncan at gmail.com
Fri Aug 5 18:53:47 CEST 2011


On 05/08/2011 12:05 PM, zhenjiang xu wrote:
> Hi R users,
>
> I have a list:
> >  x
> $A
> [1] "a"  "b"  "c"
> $B
> [1] "b"  "c"
> $C
> [1] "c"
>
> I want to convert it to a lowercase-to-uppercase list like this:
> >  y
> $a
> [1] "A"
> $b
> [1] "A"  "B"
> $c
> [1] "A"  "B"  "C"
>
> In a word, I want to reverse the list names and the elements under
> each list name. Is there any quick way to do that? Thanks

I interpreted this question differently from the others, and your 
example is ambiguous as to which is the right interpretation.  I thought 
you wanted to swap names and elements,  so

 > x <- list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d"))
 > x
$A
[1] "d" "e" "f"

$B
[1] "d" "e"

$C
[1] "d"

would become

 > list(d=c("A", "B", "C"), e=c("A", "B"), f="A")
$d
[1] "A" "B" "C"

$e
[1] "A" "B"

$f
[1] "A"

I don't know a slick way to do this; I'd just do it by brute force, 
looping over the names of x.

Duncan Murdoch



More information about the R-help mailing list