4

I am trying to count a binary character outcome by row in a large data frame:

V1      V2      V3      V4      V5  
Loss    Loss    Loss    Loss    Loss
Loss    Loss    Win     Win     Loss
Loss    Loss    Loss    Loss    Loss

What I need to know is the frequency of wins and losses by row. This is just a short example (fragment of large simulated output) but for row 1, in five simulations, I have five Losses, row two three loss and two win, etc.

I was hoping to generate either a separate table that shows the frequency of wins/loss by row or, if that won't work, add two new columns: one that provides the number of "Win" and "Loss" for each row.

Each row is a different case, and each column is a replicate of that case. This appears as a data frame of factors with two levels "Loss" "Win".

4

2 回答 2

11

这是一个快速的矢量化解决方案(假设您的数据集称为df

Loss <- rowSums(df == "Loss") # Count the "Loss" per row
cbind(Loss, Wins = ncol(df) - Loss) # Subscribe these from the columns numbers and combine
#      Loss Wins
# [1,]    5    0
# [2,]    3    2
# [3,]    5    0
于 2015-01-08T21:54:46.460 回答
1

与 base 的另一种选择R

stats = function(u){
    win = sum(u=="Win")
    data.frame(Win=win, Loss=length(u)-win)
}

Reduce(rbind, apply(df, 1, stats))

#  Win Loss
#1   0    5
#2   2    3
#3   0    5

或者更好的一行但非矢量化:

t(apply(df, 1, function(u) table(factor(u, levels=c("Win","Loss")))))

#     Win Loss
#[1,]   0    5
#[2,]   2    3
#[3,]   0    5
于 2015-01-11T13:23:16.657 回答