i have data frame multiple columns, of need change 'time' class using chron can retrieve basic statistics. these columns times stored characters , formatted this: hh:mm.
here subset of list of columns need change:
> data date flt type reg ac dep arr std sta atd ata 1 15-01-02 953 j c-gcpt 73m yvq yev 12:00 12:55 13:00 13:59 2 15-01-04 953 j c-gcpt 73m yvq yev 12:00 12:55 13:17 14:13 3 15-01-05 953 j c-gcpt 73m yvq yev 12:00 12:55 13:20 14:14 time_list <-c("std","sta","atd","ata") here have done change 1 column (and works):
data$ata <- paste0(data$ata, ':00') data$ata<-chron(times.=data$ata) class(data$ata) [1] "times" however, prefer able columns @ same time since there many of them. i've tried multiple techniques , seem work first part, pasting ':00', goes wrong second part, using chron . seem have length problem don't understand
using
dmapdata[,time_list]<- data%>% select(one_of(time_list)) %>% dmap(paste0,':00') data[,time_list]<- data %>% select(one_of(time_list)) %>% dmap(chron,times.=data[,time_list]) **error in .f(.d[[i]], ...) : .d[[i]] , data[, time_list] must have equal lengths**using
applyyevdata[,(time_list)] <- lapply(yevdata[,(time_list)], paste0,':00') data[,(time_list)] <- lapply(data[,(time_list)], chron, times. =data[,(time_list)]) **error in fun(x[[i]], ...) : x[[i]] , data[, (time_list)] must have equal lengths**using
forloop
i tried using loop, i'm beginner , anywhere.
- using "simple" solution stack overflow question.
it made mess, pasting.
efficiently transform multiple columns of data frame
any ideas in plain beginner language appreciated! if possible nest both operations, better!
dplyr::mutate_at work situation. define variables want mutate , define function want use.
you can pasting , converting time in single step within funs using . notation , nesting functions.
library(dplyr) data = mutate_at(data, time_list, funs(chron(times. = paste0(., ":00"))))
Comments
Post a Comment