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".
Here's a quick vectorized solution (assuming your data set called 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With