Suppose we have a data frame df like
book_id book_category book_word_hi book_word_bye book_word_yes
1 drama 3 0 4
2 action 1 4 5
3 drama 5 3 2
I would like to count the number of values within the book_word columns and sum them in a table for each book_category.
So the output here should look something like:
drama: 17
action: 10
Does anyone know how to do this?
>Solution :
This is a short and simple one-liner in base R, without requiring any additional packages.
tapply(rowSums(df[3:5]), df[2], sum)
#> book_category
#> action drama
#> 10 17