

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 lowercasetouppercase 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

Best,
Zhenjiang
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


There are a few moving pieces to do this:
1) the toupper and tolower functions. These can do almost everything you
need:
x = lapply(x,toupper) ## This changes the things inside x but won't change
the names
2) Now, there are two ways to think about getting the elements from one list
to another: I'd prefer think about changing the list names and order while
leaving the elements in the lists (as for big data it will be more memory
efficient) but you could work on actually moving things.
Here it's easy:
first reverse
x = rev(x)
now you have the problem that "c" is the first element of the list so you
just need to reverse the whole list and you might as well make them
lowercase while you are at it
names(x) = tolower(names(x)[3:1]) # 3:1 will reorder the list, use length(x)
more generally
Altogether,
x = rev(lapply(x,toupper))
names(x) = tolower(names(x)[3:1])
gives us
>x
$a
[1] "C"
$b
[1] "B" "C"
$c
[1] "A" "B" "C"
as desired.
By the way, this is a very odd request: are you sure this is really what you
need to do? If you describe the more general framework, we can probably help
with some more efficient data management.
Hope this helps,
Michael Weylandt
On Fri, Aug 5, 2011 at 12:05 PM, zhenjiang xu < [hidden email]>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 lowercasetouppercase 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
> 
> Best,
> Zhenjiang
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On Aug 5, 2011, at 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 lowercasetouppercase 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
The obvious way would be:
names(lll) < to.upper(names(lll)
lll < lapply(lll, to.lower)
(If you had offered code that would construct the list, I would have
tested it.)

David Winsemius, MD
West Hartford, CT
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


toupper()/tolower()
are the functions to convert the letters.
lapply()
can be used to apply this to different list elements and
names()
is helpfull to convert the names of your list.
HTH
Jannis
On 08/05/2011 06: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 lowercasetouppercase 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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 lowercasetouppercase 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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Exactly! Sorry I get others misunderstood. The uppercase/lowercase is
only a toy example (and a bad one; yours is better than mine). My
question is a more general one: a list is basically a onetomany
matching, from the names of a list to the elements belonging to each
name. I'd like to reverse the matching, from all the elements to the
names of the list.
On Fri, Aug 5, 2011 at 12:53 PM, Duncan Murdoch
< [hidden email]> wrote:
> 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 lowercasetouppercase 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
>

Best,
Zhenjiang
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Hi:
Your clarification suggests Duncan was on the right track, so how about this:
x < list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d"))
x2 < unique(unlist(x))
w < lapply(x, function(u) names(x)[which(x2 %in% u)])
names(w) < x2
w
$d
[1] "A" "B" "C"
$e
[1] "A" "B"
$f
[1] "A"
HTH,
Dennis
On Fri, Aug 5, 2011 at 10:04 AM, zhenjiang xu < [hidden email]> wrote:
> Exactly! Sorry I get others misunderstood. The uppercase/lowercase is
> only a toy example (and a bad one; yours is better than mine). My
> question is a more general one: a list is basically a onetomany
> matching, from the names of a list to the elements belonging to each
> name. I'd like to reverse the matching, from all the elements to the
> names of the list.
>
> On Fri, Aug 5, 2011 at 12:53 PM, Duncan Murdoch
> < [hidden email]> wrote:
>> 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 lowercasetouppercase 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
>>
>
>
>
> 
> Best,
> Zhenjiang
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Here is one approach, whether it is better than the basic loop or not is up to you:
> x < list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d"))
>
> tmp < unlist(x)
> tmp2 < sub( '[09]+$', '', names(tmp) )
>
> x.new < split( tmp2, tmp )
> x.new
$d
[1] "A" "B" "C"
$e
[1] "A" "B"
$f
[1] "A"
Of course this version will have some problems if the names of your list elements end with digits that you don't want stripped off (but you can work around that by preprocessing the list names).

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111
> Original Message
> From: [hidden email] [mailto:rhelpbounces@r
> project.org] On Behalf Of zhenjiang xu
> Sent: Friday, August 05, 2011 11:04 AM
> To: Duncan Murdoch
> Cc: rhelp
> Subject: Re: [R] a question on list manipulation
>
> Exactly! Sorry I get others misunderstood. The uppercase/lowercase is
> only a toy example (and a bad one; yours is better than mine). My
> question is a more general one: a list is basically a onetomany
> matching, from the names of a list to the elements belonging to each
> name. I'd like to reverse the matching, from all the elements to the
> names of the list.
>
> On Fri, Aug 5, 2011 at 12:53 PM, Duncan Murdoch
> < [hidden email]> wrote:
> > 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 lowercasetouppercase 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
> >
>
>
>
> 
> Best,
> Zhenjiang
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/posting> guide.html
> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


This is a nice solution. Thanks, Dennis. But I am afraid if the length
of the list x isn't equal to the length of x2, there will be errors
since lapply returns a list of the same length.
> x < list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d","g"))
> x2 < unique(unlist(x))
> w < lapply(x, function(u) names(x)[which(x2 %in% u)])
> names(w) < x2
Error in names(w) < x2 :
'names' attribute [4] must be the same length as the vector [3]
On Fri, Aug 5, 2011 at 3:23 PM, Dennis Murphy < [hidden email]> wrote:
> Hi:
>
> Your clarification suggests Duncan was on the right track, so how about this:
>
> x < list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d"))
> x2 < unique(unlist(x))
> w < lapply(x, function(u) names(x)[which(x2 %in% u)])
> names(w) < x2
> w
> $d
> [1] "A" "B" "C"
>
> $e
> [1] "A" "B"
>
> $f
> [1] "A"
>
> HTH,
> Dennis
>
> On Fri, Aug 5, 2011 at 10:04 AM, zhenjiang xu < [hidden email]> wrote:
>> Exactly! Sorry I get others misunderstood. The uppercase/lowercase is
>> only a toy example (and a bad one; yours is better than mine). My
>> question is a more general one: a list is basically a onetomany
>> matching, from the names of a list to the elements belonging to each
>> name. I'd like to reverse the matching, from all the elements to the
>> names of the list.
>>
>> On Fri, Aug 5, 2011 at 12:53 PM, Duncan Murdoch
>> < [hidden email]> wrote:
>>> 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 lowercasetouppercase 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
>>>
>>
>>
>>
>> 
>> Best,
>> Zhenjiang
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>>
>

Best,
Zhenjiang
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Unfortunately the list names of my real data are irregular with mixed
digit and letters at the end. This is good idea though. It inspired me
to give another solution based on that:
> x < list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d","g"))
> tmp < unlist(x, use.names=F)
> a = unlist(lapply(x, length))
> tmp2 = rep(names(a), a)
> x.new = split(tmp2, tmp)
And I tested it on my data. It took over an hour using for loops while
finishing in a second with the vectorization. Thanks all of you.
Hooray~
On Fri, Aug 5, 2011 at 3:31 PM, Greg Snow < [hidden email]> wrote:
> Here is one approach, whether it is better than the basic loop or not is up to you:
>
>> x < list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d"))
>>
>> tmp < unlist(x)
>> tmp2 < sub( '[09]+$', '', names(tmp) )
>>
>> x.new < split( tmp2, tmp )
>> x.new
> $d
> [1] "A" "B" "C"
>
> $e
> [1] "A" "B"
>
> $f
> [1] "A"
>
>
> Of course this version will have some problems if the names of your list elements end with digits that you don't want stripped off (but you can work around that by preprocessing the list names).
>
> 
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> [hidden email]
> 801.408.8111
>
>
>> Original Message
>> From: [hidden email] [mailto:rhelpbounces@r
>> project.org] On Behalf Of zhenjiang xu
>> Sent: Friday, August 05, 2011 11:04 AM
>> To: Duncan Murdoch
>> Cc: rhelp
>> Subject: Re: [R] a question on list manipulation
>>
>> Exactly! Sorry I get others misunderstood. The uppercase/lowercase is
>> only a toy example (and a bad one; yours is better than mine). My
>> question is a more general one: a list is basically a onetomany
>> matching, from the names of a list to the elements belonging to each
>> name. I'd like to reverse the matching, from all the elements to the
>> names of the list.
>>
>> On Fri, Aug 5, 2011 at 12:53 PM, Duncan Murdoch
>> < [hidden email]> wrote:
>> > 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 lowercasetouppercase 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
>> >
>>
>>
>>
>> 
>> Best,
>> Zhenjiang
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide http://www.Rproject.org/posting>> guide.html
>> and provide commented, minimal, selfcontained, reproducible code.
>

Best,
Zhenjiang
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On 8/6/2011 9:21 AM, zhenjiang xu wrote:
> Unfortunately the list names of my real data are irregular with mixed
> digit and letters at the end. This is good idea though. It inspired me
> to give another solution based on that:
>
>> x< list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d","g"))
>> tmp< unlist(x, use.names=F)
>> a = unlist(lapply(x, length))
>> tmp2 = rep(names(a), a)
>> x.new = split(tmp2, tmp)
>
> And I tested it on my data. It took over an hour using for loops while
> finishing in a second with the vectorization. Thanks all of you.
> Hooray~
Coming at this late, and after you already have a solution, but here is
one using plyr:
library("plyr")
x < list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d"))
tmp < ldply(x, function(x) {data.frame(v=unlist(x))})
dlply(tmp, .(v), function(x) {x[[".id"]]})
Or it could be combined into a single line:
dlply(ldply(x, function(x) {data.frame(v=unlist(x))}), .(v), function(x)
{x[[".id"]]})
These will carry a few extra attributes you don't necessarily need, but
don't really hurt anything. I don't know how these compare timing wise
with the other solutions.
The basic logic is turn the list into a data.frame with an ".id" column
(original names of lists) and a "v" column (entries in original lists),
then reaggregate this by "v" listing the ".id"'s.
> On Fri, Aug 5, 2011 at 3:31 PM, Greg Snow< [hidden email]> wrote:
>> Here is one approach, whether it is better than the basic loop or not is up to you:
>>
>>> x< list(A=c("d", "e", "f"), B=c("d", "e"), C=c("d"))
>>>
>>> tmp< unlist(x)
>>> tmp2< sub( '[09]+$', '', names(tmp) )
>>>
>>> x.new< split( tmp2, tmp )
>>> x.new
>> $d
>> [1] "A" "B" "C"
>>
>> $e
>> [1] "A" "B"
>>
>> $f
>> [1] "A"
>>
>>
>> Of course this version will have some problems if the names of your list elements end with digits that you don't want stripped off (but you can work around that by preprocessing the list names).
>>
>> 
>> Gregory (Greg) L. Snow Ph.D.
>> Statistical Data Center
>> Intermountain Healthcare
>> [hidden email]
>> 801.408.8111
>>
>>
>>> Original Message
>>> From: [hidden email] [mailto:rhelpbounces@r
>>> project.org] On Behalf Of zhenjiang xu
>>> Sent: Friday, August 05, 2011 11:04 AM
>>> To: Duncan Murdoch
>>> Cc: rhelp
>>> Subject: Re: [R] a question on list manipulation
>>>
>>> Exactly! Sorry I get others misunderstood. The uppercase/lowercase is
>>> only a toy example (and a bad one; yours is better than mine). My
>>> question is a more general one: a list is basically a onetomany
>>> matching, from the names of a list to the elements belonging to each
>>> name. I'd like to reverse the matching, from all the elements to the
>>> names of the list.
>>>
>>> On Fri, Aug 5, 2011 at 12:53 PM, Duncan Murdoch
>>> < [hidden email]> wrote:
>>>> 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 lowercasetouppercase 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
>>>>
>>>
>>>
>>>
>>> 
>>> Best,
>>> Zhenjiang
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/rhelp>>> PLEASE do read the posting guide http://www.Rproject.org/posting>>> guide.html
>>> and provide commented, minimal, selfcontained, reproducible code.
>>
>
>
>

Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

