r - How to create multiple rules for "lapply" function -
r - How to create multiple rules for "lapply" function -
currently, have array of list(groupa) next example:
$aaaaaa time timegap 1 06:00:00 0 2 07:00:00 60 3 08:00:00 40 4 09:00:00 0 5 10:00:00 30 $bbbbbbb time timegap 1 06:00:00 0 2 07:00:00 60 3 08:00:00 40 4 09:00:00 0 5 10:00:00 30
i trying create function generate dummy variable if timegap greater number. challenge number generating dummy variable different others if time in in range 07:00:00 09:00:00.
what did following:
dummytime<-function(x){ if(x$time>times("07:00:00") & x$time<times("09:00:00")){ d<-c(1200) } else{ d<-c(600) } dummytime<- as.numeric(x$timegap>=d) as.data.frame(dummytime) } dumtime<-lapply(groupm2,dummytime)
however, got error this:
error in if (as.logical(x$time > times("07:00:00") & x$time < times("09:00:00"))) { : missing value true/false needed
any suggestion? help in advance.
here 1 way. since used chron
bundle convert character time. have done that. then, created list. then, lpply
.
library(chron) # time class 'times' df1$time <- chron(times = df1$time) df2$time <- chron(times = df2$time) # create list ana <- list(df1 = df1, df2 = df2) #$df1 # time timegap #1 06:00:00 0 #2 07:00:00 60 #3 08:00:00 40 #4 09:00:00 0 #5 10:00:00 30 lapply(ana, function(x){ x$test <- ifelse(x$time >= "07:00:00" & x$time <= "09:00:00", 1200, 600) x }) #$df1 # time timegap test #1 06:00:00 0 600 #2 07:00:00 60 1200 #3 08:00:00 40 1200 #4 09:00:00 0 1200 #5 10:00:00 30 600 #$df2 # time timegap test #1 06:00:00 0 600 #2 07:00:00 60 1200 #3 08:00:00 40 1200 #4 09:00:00 0 1200 #5 10:00:00 30 600
or
lapply(ana, transform, test = ifelse(time >= "07:00:00" & time <= "09:00:00", 1200, 600))
r lapply
Comments
Post a Comment