I have a dataframe:
id cog com emo
AUD-002 12 34 24
PAR-044 NA 28 38
BRE-019 0 NA 51
2-1-GRE NA 31 68
I am interested in counting non-NA values per row between all pairs of columns cog, com, emo
My required output is:
id cog com emo cog-com cog-emo com-emo
AUD-002 12 34 24 1 1 1
PAR-044 NA 28 38 0 0 1
BRE-019 0 NA 51 0 1 0
2-1-GRE NA 31 68 0 0 1
I found that the following question might be related: Count non-NA observations by row in selected columns but they count overall non-NA entries per row and not by pairs of columns of that row. Also, I can achieve this by using multiple statements like this:
library(dplyr)
df = df %>%
mutate(count_cog_com = rowSums(!is.na(select(., 2:3))) - 1)
df = df %>%
mutate(count_cog_emo = rowSums(!is.na(select(., 2,4))) - 1)
df = df %>%
mutate(count_com_emo = rowSums(!is.na(select(., 3:4))) - 1)
But I don't want to use these on my actual data because I have several columns. Is there an easy dplyr
way to achieve this functionality? Can these statements be joined somehow? Thank you fo your help!
The dput is as below:
dput(df)
structure(list(id = structure(c(2L, 4L, 3L, 1L),
.Label = c("2-1-GRE", "AUD-002", "BRE-019", "PAR-044"),
class = "factor"),
cog = c(12L, NA, 0L, NA),
com = c(34L, 28L, NA, 31L),
emo = c(24L, 38L, 51L, 68L)),
row.names = c(NA, -4L), class = "data.frame")