Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two list components

Tags:

merge

list

r

I have a big list, but micro example would be like the following:

A <- c("A", "a", "A", "a", "A")
B <- c("A", "A", "a", "a", "a")
C <- c(1, 2, 3, 1, 4) 
mylist <- list(A=A, B=B, C= C)

expected output is merge A with B so that each component will look like AB

AA, aA, Aa, aa, Aa

better should be sorted, upper case is always first

AA, Aa, Aa, aa, Aa

Thus new list or matrix should have two columns or rows:

AA, Aa, Aa, aa, Aa
1,   2, 3,   1, 4

Now I want calculate average of C based on class - "AA", "Aa", and "aa"

Looks simple but I could not figure out easily.

like image 496
jon Avatar asked Oct 14 '11 01:10

jon


2 Answers

> (ab <- paste(A, B, sep="") )
[1] "AA" "aA" "Aa" "aa" "Aa"
> (ab <- paste(A, B, sep="") )  # the joining step
[1] "AA" "aA" "Aa" "aa" "Aa"
> (ab <- sub("([a-z])([A-Z])", "\\2\\1", ab) ) # swap lowercase uppercase
[1] "AA" "Aa" "Aa" "aa" "Aa"

> rbind(ab, C)                  # matrix
   [,1] [,2] [,3] [,4] [,5]
ab "AA" "Aa" "Aa" "aa" "Aa"
C  "1"  "2"  "3"  "1"  "4" 
> data.frame(alleles=ab, count=C)  # dataframes are lists
  alleles count
1      AA     1
2      Aa     2
3      Aa     3
4      aa     1
5      Aa     4
like image 82
IRTFM Avatar answered Nov 05 '22 23:11

IRTFM


I can do it if your data is arranged in a data.frame using the package plyr

> A <- c("A", "a", "A", "a", "A")
> B <- c("A", "A", "a", "a", "a")
> C <- c(1, 2, 3, 1, 4) 
> groups <- sort(paste(A, B, sep=""))
[1] "AA" "aA" "Aa" "aa" "Aa"
> my.df <- data.frame(A=A, B=B, C=C, group=groups)

> require(plyr)
> result <- ddply(my.df, "group", transform, group.means=mean(C))
> result[order(result$group, decreasing=TRUE),]
  A B C group group.means
5 A A 1    AA         1.0
3 A a 3    Aa         3.5
4 A a 4    Aa         3.5
2 a A 2    aA         2.0
1 a a 1    aa         1.0
like image 41
Ryogi Avatar answered Nov 05 '22 23:11

Ryogi