I create a dataframe df.
df <- data.frame (id = 1:10,
var1 = 10:19,
var2 = sample(c(1:2,NA), 10, replace=T),
var3 = sample(c(3:5, NA), 10, replace=T))
What I need is a new column var4, which count the number of non-NA values of each row (excluding the id column). So for example, if a row is like var1=19, var2=1, var3=NA, then var4=2. I could not find a good way to do this in dplyr. something like:
df %in% mutate(var4= ... )
I appreciate if anyone can help me with that.
df$var4 <- rowSums(!is.na(df[-which(names(df)=="id")]))
– Dm