Reputation: 1099
I'm applying user defined function in do(), but for some cases I don't need to do a calculation (to catch log(<0)). What should my function return? NULL nor () doesn't help.
my_function<-function(data) {
if(data$a<0) {
return(NULL) #?????????
} else {
return(data.frame(ln=log(data$a)))
}
}
table<-data.frame(a=seq(10,-10,by=-1),b=seq(0,20,by=1))
result<-table %>%
group_by(b) %>%
do(
my_function(data=.)
)
Upvotes: 4
Views: 520
Reputation: 7307
my_function<-function(data) {
if(data$a<0) {
return(data.frame(NULL))
} else {
return(data.frame(ln=log(data$a)))
}
}
Upvotes: 2