r - If function in dplyr::mutate : the condition has length > 1 -


a lot of people seem have issue not able find satisfying answer. if indulge me, sure understand what's happening

i'm having dates of various format in dataframe (also common issue) have built small function handle me:

datehandler <- function(inputstring){   if(grepl("-",inputstring)==t){     lubridate::dmy(inputstring, tz="gmt")   }else{     as.posixct(as.numeric(inputstring)*60*60*24, origin="1899-12-30", tz="gmt")   } } 

when using on 1 element works fine:

myexample <-c("18-mar-11","42433")  > datehandler(myexample[1]) [1] "2011-03-18 gmt" > datehandler(myexample[2]) [1] "2016-03-04 gmt" 

however when using on whole column not work:

mydf <- as.data.frame(myexample) > mydf <- mydf %>%  +   dplyr::mutate(dateclean=datehandler(myexample)) warning messages: 1: in if (grepl("-", inputstring) == t) { :   condition has length > 1 , first element used 2:  1 failed parse.  

from reading on forum, current understanding r passes vector elements of mydf$myexample function, not built handle vector of length >1. if correct, next step understand there. many people recommend using ifelse rather if not understand how me. read ifelse returns of same format input, not work me in case.

thank in advance answering question 10000th time.

nicolas

you have 2 option on go there. 1 apply current function list using lapply. in:

mydf$dateclean <- lapply(mydf$myexample, function(x) datehandler(x))

the other option build vectorized function designed take vector input rather single data point. here simple example:

datehandlervectorized <- function(inputvector){    output <- rep(as.posixct("1/1/11"), length(inputvector))   useluridate <- grepl("-", inputvector)   output[useluridate] <- lubridate::dmy(inputvector[useluridate], tz="gmt")   output[!useluridate] <- as.posixct(as.numeric(inputvector[!useluridate])*60*60*24, origin="1899-12-30", tz="gmt")   output  }  mydf <- mydf %>% dplyr::mutate(dateclean=datehandlervectorized(mydf$myexample)) 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -