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.
> (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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With