I'm trying to make a new data frame based on an ifelse statement. I'm checking in 1 dataframe to see whether a field contains a string. If it does, I then want to add rows from that dataframe to another data frame, using filter, subset, and rbind methods:
This works:
#Check to see if field contains string.
timeline.data.2 <-ifelse(grepl(1043-1, prescription.data$case_id),
timeline.data <- timeline.data %>%
filter(case_id==1043-1),
"do nothing")
This does not:
#Check to see if field contains string.
timeline.data.2 <-ifelse(grepl(1043-1, prescription.data$case_id),
timeline.data <- timeline.data %>%
filter(case_id==1043-1),
timeline.data <-subset(timeline.data, select = c(event,group,start,end,color,tooltip)),
timeline.data <- subset(prescription.data, select = c(event,group,start,end,color,tooltip)),
timeline.data <- rbind(timeline.data, prescription.data),
"do nothing")
I'm getting the error:
Error in ifelse(grepl(1043 - 1, prescription.data$case_id), timeline.data <- timeline.data %>% :
unused argument ("do nothing")
What am I doing wrong here?
It may be better to use if/else
library(dplyr)
if(any(grepl("1043-1", prescription.data$case_id))) {
timeline.data <- timeline.data %>%
filter(grepl("1043-1", case_id))
} else "do nothing"