been stuck on this for a while.
For every row, I'm trying to count across the columns for all values greater than 0. But the caveat is that I need to specify the starting column to start counting across using each row's specific value in another column.
For example the table would look like this:
ID | StartWeek | 1 | 2 | 3 |
123 2 3 0 1
456 1 1 0 1
Expected output would look like this:
ID | StartWeek | 1 | 2 | 3 | CountRow |
123 2 3 0 1 1
456 1 1 0 1 2
I tried something like this:
df <- df %>%
mutate(CountRow = rowSums(.[StartWeek:5] > 0))
But it just gives me the entire column instead of the individual value of each row. I think I read upon a potential solution using groupby() but would there by another way of doing this just by accessing every rows certain value instead of calling the entire column.