I used
df$Total.P.n <- rowSums(df[grep('p.n', names(df), ignore.case = FALSE)])
to sum count values from any column name containing p.n
, but the values it produced are way off. The columns are counts of certain combinations of language types in a language corpus. I want to get a summary of all times p.n
. was used within other combinations, but am struggling. It seems like perhaps it is counting other occurences like e.sp.NR
in my variable names, but shouldn't ignore.case=FALSE
take care of that? I've also tried tidyverse
and dplyr
solutions to no avail.
Here's example of df
structure:
ID. | do.p.n.NP | do.p.n.SE | p.d.e.sp.SR |
---|---|---|---|
1510 | 4 | 6 | 2 |
1515 | 2 | 0 | 1 |
and what I need:
ID. | do.p.n.NP | do.p.n.SE | p.d.e.sp.SR | Total.P.n |
---|---|---|---|---|
1510 | 4 | 6 | 2 | 10 |
1515 | 2 | 0 | 1 | 2 |