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 -- Best, Zhenjiang
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 <zhenjiang.xu@gmail.com>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 > -- > Best, > Zhenjiang > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
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 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? ThanksThe 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
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 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
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? ThanksI 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