On 2020-08-02 09:24 -0700, Philip wrote: | Below is some Weather Service data. I | would like to parse the forecast date | field into four different columns: | Year, Month, Day, Hour
Dear Philip, I'm largely re-iterating Eric and Jeff's excellent solutions: > dat <- structure(list(forecast.date = + c("2020-08-01 12:00:00", + "2020-08-01 12:00:00", + "2020-08-01 12:00:00", + "2020-08-01 12:00:00", + "2020-08-01 12:00:00" + ), TMP = c("305.495", "305.245", + "305.057", "305.745", "305.495" + )), row.names = c(NA, 5L), + class = "data.frame") > t(apply(simplify2array( + strsplit(dat$forecast.date, "-| |:")), + 2, as.numeric)) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 2020 8 1 12 0 0 [2,] 2020 8 1 12 0 0 [3,] 2020 8 1 12 0 0 [4,] 2020 8 1 12 0 0 [5,] 2020 8 1 12 0 0 > simplify2array(parallel::mclapply(c( + lubridate::year, + lubridate::month, + lubridate::day, + lubridate::hour), function(FUN, x) { + FUN(x) + }, x=dat$forecast.date)) [,1] [,2] [,3] [,4] [1,] 2020 8 1 12 [2,] 2020 8 1 12 [3,] 2020 8 1 12 [4,] 2020 8 1 12 [5,] 2020 8 1 12 V r
signature.asc
Description: PGP signature
______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.