I have a data frame where I'm trying to count the number of non NA values
csv <- "A,B,C,D,E,F,G
NA,NA,1,NA,NA,NA,NA
0,NA,0,NA,1,NA,1
NA,1,1,NA,0,NA,NA
0,1,1,0,NA,NA,0"
temp <- read.csv(text=csv)
temp %>% mutate(twopresent = ifelse((!is.na(A)+!is.na(B)+!is.na(C)) >= 2, TRUE, FALSE),
total = sum(A,B,C, na.rm=TRUE) )
I expect to see:
A B C D E F G twopresent total
1 NA NA 0 NA NA NA NA FALSE 0
2 0 NA 0 NA 1 NA 1 TRUE 0
3 NA 1 1 NA 0 NA NA TRUE 2
4 0 1 0 0 NA NA 0 TRUE 1
But get:
A B C D E F G twopresent total
1 NA NA 1 NA NA NA NA FALSE 5
2 0 NA 0 NA 1 NA 1 FALSE 5
3 NA 1 1 NA 0 NA NA FALSE 5
4 0 1 1 0 NA NA 0 FALSE 5
rowSums solution in dplyr:
temp %>% mutate(twopresent =
ifelse(rowSums(!is.na(A), !is.na(B), !is.na(C)) >= 2,
TRUE,
FALSE),
total = rowSums(A,B,C, na.rm=TRUE) )
gives:
Error: 'x' must be an array of at least two dimensions
rowSumsas inrowSums(!is.na(temp[c("A", "B", "C")]))totalcolumn, it's simplyrowSums(temp[1:3], na.rm = TRUE)(you've messed up your desired output a bit). This is why you should learn R before you learn dplyrtotalis correct?