This one is breaking my brain:
I have a data.frame (or matrix if need be) of individuals (A-D) that have fought one another. Winners are on rows and losers in columns in a matrix. I need to calculate (for each winner), how many times their losers won other battles:
| A | B | C | D
A| 0 | 0 | 1 | 1
B| 1 | 0 | 1 | 0
C| 0 | 0 | 0 | 1
D| 0 | 1 | 1 | 0
So I would like to create variable L (loser wins) such that for each individual, if he/she beat an another individual, their row is summed and added to a total. In this example, the new variable would look like this:
| A | B | C | D | L
A| 0 | 0 | 1 | 1 | 3 (1 win from C, 2 from D)
B| 1 | 0 | 1 | 0 | 3 (2 wins from A, 1 from C)
C| 0 | 0 | 0 | 1 | 2 (2 wins from D)
D| 0 | 1 | 1 | 0 | 3 (2 wins from B, 1 from D)
I have tried a for loop and have managed to get rowSums for each individual using:
l=c()
for (i in 1:nrow(df)){
l[i]=rowSums(df[i,])
}
loserwinners=data.frame(l,row.names=rownames(df))
I know, gold star, whoopee. But I don't know the logical statement to call within a matrix if a value is non-zero, match that column name to the row name and add that row's values to a total. I would appreciate any leads on how to do this, and apologies if the syntax is incorrect!
Aucun commentaire:
Enregistrer un commentaire