Let's say I have the following df
test = read.table(text = "total_score_1 total_score_2 partner_total_score_1 total_score_3 total_score_4 letter
1 -1 1 1 -1 B
1 1 1 -1 1 C
-1 -1 -1 -1 1 A", header = T)
I'd like to match all columns that contain "total_score" but NOT the word "partner" and then create a new measure that sums across all "total_score" columns, treating the -1 as 0.
I'm able to take a basic rowSum like so
mutate(net_correct = rowSums(select(., grep("total_score", names(.))))
Note, however, this does not exclude the possibility of matching the word "partner", which I wasn't able to find out how to do within a single grep
command.
However, I'd now like to create a total_correct
value which is a rowSum on the same columns except the -1s are treated as 0s.
This would result in a data.frame like so:
total_score_1 total_score_2 partner_total_score_1 total_score_3 total_score_4 letter total_sum
1 1 -1 1 1 -1 B 2
2 1 1 1 -1 1 C 3
3 -1 -1 -1 -1 1 A 1
One way might be to just count the total number of "1s" (rather than actually doing a sum), but I could not figure out how to do so within a mutate command