Combining two list components

I have a big list, but a micro example will look like this:

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) 

the expected conclusion is the merger of A with B, so that each component will look like AB

 AA, aA, Aa, aa, Aa 

better sort, uppercase always first

 AA, aA, Aa, aa, Aa 

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

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

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

It looks simple, but I could not easily understand.

+6
source share
3 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("([az])([AZ])", "\\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 
+2
source

I can do this if your data is located in data.frame using plyr package

 > 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),] ABC group group.means 5 AA 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 aa 1 aa 1.0 
+2
source

With your data:

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

I define a data.frame using a combination of A and B as the key column:

 AB <- paste(A, B, sep='') df <- data.frame(id=AB, C=C) > df id C 1 AA 1 2 aA 2 3 Aa 3 4 aa 1 5 Aa 4 

If you need to order this data.frame before aggregation, then:

 df <- df[order(AB, decreasing=TRUE),] > df id C 1 AA 1 3 Aa 3 5 Aa 4 2 aA 2 4 aa 1 

And with aggregate you calculate the average for each id :

 meanDF <- aggregate(C~id, data=df, mean) > meanDF id C 1 aa 1.0 2 aA 2.0 3 Aa 3.5 4 AA 1.0 

But if you want to order after aggregation, then:

 df <- data.frame(id=AB, C=C) meanDF <- aggregate(C~id, data=df, mean) meanDF <- meanDF[order(meanDF$id, decreasing=TRUE),] 
+1
source

Source: https://habr.com/ru/post/899266/


All Articles