I need to sum some columns in a data.frame with a rule that says, a column is to be summed to NA
if more than one observation is missing NA
if only 1 or less missing it is to be summed regardless.
Say I have some data like this,
dfn <- data.frame(
a = c(3, 3, 0, 3),
b = c(1, NA, 0, NA),
c = c(0, 3, NA, 1))
dfn
a b c
1 3 1 0
2 3 NA 3
3 0 0 NA
4 3 NA 1
and I apply my rule, and sum the columns with less then 2 missing NA
. So I get something like this.
a b c
1 3 1 0
2 3 NA 3
3 0 0 NA
4 3 NA 1
5 9 NA 4
I've played around with colSums(dfn, na.rm = FALSE)
and colSums(dfn, na.rm = TRUE)
. In my real data there is more then three columns and also more then 4 rows. I imagine I can count the missing some way and use that as a rule?