Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count occurrences of a string, by row, in a large data frame

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".

like image 276
mike Avatar asked Dec 14 '22 17:12

mike


1 Answers

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
like image 149
David Arenburg Avatar answered Jan 13 '23 13:01

David Arenburg