I'm trying to use rowwise
and mutate
with a function that can return an NA value, but am getting an error. Consider this contrived example (which obviously is not a case for rowwise, but illustrates the error):
This works:
library(dplyr)
data.frame(k=rnorm(10)) %>% mutate(l = ifelse(k > 0, 1, NA))
k l
1 -1.40223573 NA
2 1.47581873 1
3 0.35938746 1
4 0.87240448 1
5 -0.61991535 NA
6 -1.88152820 NA
7 0.25837350 1
8 -0.02250385 NA
9 0.92757943 1
10 0.49023792 1
But this gives an error:
library(dplyr)
data.frame(k=rnorm(10)) %>% rowwise() %>% mutate(l = ifelse(k > 0, 1, NA))
Error: incompatible types, expecting a numeric vector