Reputation: 13
I need some help for my R project. I want to make a matrix based on the average Relative Cleavage Intensity (RCI). This is my raw data(mydata = 632 obs. Of 3 variables):
R.Int amino1 amino2
1 14 W I
2 0 K E
3 79 Q I
4 80 Q I
5 100 K V
6 100 K V
7 100 K P
8 24 K P
9 100 K I
10 100 K I
11 100 K I
12 100 K I
13 100 K I
14 100 K E
15 4 H E
16 0 H E
17 0 F Y
18 0 F Y
19 2 E Q
20 2 E Q
I have managed so far to calculate the average RCI for all possible amino acid combinations by doing this code:
for (i in x) {
for (j in x) {
sub <- subset(mydata, mydata$amino1==LETTERS[i] & mydata$amino2==LETTERS[j])
g <- group_by(sub, amino1) %>% summarize(m = mean(R.Int))
h <- group_by(sub, amino2) %>% summarize(m = mean(R.Int))
c <- cbind(g,h)
d <- subset(c, select = -4)
neworder <- d[, c(1,3,2)]
n <- setnames(neworder, "m", "R.Int")
if (nrow(sub) > 0)
print(n)
}
}
The above code gives this output:
amino1 amino2 R.Int
1 A L 25
amino1 amino2 R.Int
1 A E 41
amino1 amino2 R.Int
1 A R 1.25
amino1 amino2 R.Int
1 A K 80
amino1 amino2 R.Int
1 A S 4.1
amino1 amino2 R.Int
1 G G 12
amino1 amino2 R.Int
1 G L 7
amino1 amino2 R.Int
1 G W 2
amino1 amino2 R.Int
1 G Y 0
amino1 amino2 R.Int
1 G D 0.4
My question is: how do I build a matrix on that? I want the matrix to look like this with the average RCI values at each possible amino acid combination:
A G I L P V F W Y D Q E R H K S T C M N
N NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
M NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
c NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
T NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
S NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
K NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
H NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
R NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
E NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Q NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
D NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Y NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
W NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
F NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
V NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
P NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
L NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
I NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
G NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
A NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
I really hope you can help me, thanks in advance
Upvotes: 1
Views: 60
Reputation: 38500
Here is one way you can accomplish this:
# construct example data frame
set.seed(1234)
df <- data.frame(R.int=sample(1:100, 25, replace=T), amino1=sample(letters[1:5], 25, replace=T), amino2=sample(letters[1:5], 25, replace=T), stringsAsFactors=F)
# make sure that amino1 and amino2 are character variables (not factors)
df$amino1 <- as.character(df$amino1)
df$amino2 <- as.character(df$amino2)
# calculate mean for each amino acid combination
dfAgg <- aggregate(cbind("R.Int"=R.Int)~amino1 + amino2, data=df, FUN=mean)
# construct matrix for storage, give it row and column names of amino acids
myMat <- matrix(0, nrow=length(unique(dfAgg$amino1)), ncol=length(unique(dfAgg$amino2)))
rownames(myMat) <- unique(dfAgg$amino1)
colnames(myMat) <- unique(dfAgg$amino2)
# fill in the values
myMat[cbind(dfAgg$amino1, dfAgg$amino2)] <- dfAgg$R.Int
Note that your amino acid variables must be of type character (ie, not factors) for this method to work. The final line uses the combinations of the names of the amino acids as they appear in the dfAgg data.frame as indices to fill in the final matrix.
Upvotes: 1