I have the following data frame:
Gene <- c("1","2","3","4","5","6")
> A1.1 <- c(1,1,2,4,3,5)
> A1.2 <- c(1,2,3,4,5,6)
> B1.1 <- c(2,2,3,5,5,5)
> B1.2 <- c(1,2,3,5,5,5)
> A2.1 <- c(3,2,5,6,6,6)
> A2.2 <- c(1,1,2,2,4,6)
> B2.1 <- c(2,1,4,5,7,4)
> B2.2 <- c(1,3,4,5,2,3)
> df <- data.frame(Gene,A1.1,A1.2,B1.1,B1.2,A2.1,A2.2,B2.1,B2.2)
> df
Gene A1.1 A1.2 B1.1 B1.2 A2.1 A2.2 B2.1 B2.2
1 1 1 1 2 1 3 1 2 1
2 2 1 2 2 2 2 1 1 3
3 3 2 3 3 3 5 2 4 4
4 4 4 4 5 5 6 2 5 5
5 5 3 5 5 5 6 4 7 2
6 6 5 6 5 5 6 6 4 3
I wish to calculate the average across samples (columns) of the same letter/number for each gene (row).
ie. calculate the average of each gene (#1-6) for both A1 samples, both A2 samples, both B1 samples and both B2 samples.
I know I can do this the long way using apply()
Ex,
> A1_df <- data.frame(df$A1.1, df$A1.2)
> A1 <- apply(A1_df, 1, mean)
> A1
[1] 1.0 1.5 2.5 4.0 4.0 5.5
But is there a shortcut way of doing this using sapply() such that I end up with a new data frame where the columns are now "A1", "A2", "B1", "B2"?
Let me know if anything is unclear
Thanks