Parsing a Date
Hello,
And another solution, taking advantage of Rasmus' one:
simplify2array(parallel::mclapply(c(
? "%Y",
? "%m",
? "%d",
? "%H"), function(fmt, x) {
??? as.integer(format(as.POSIXct(x), format = fmt))
}, x = dta$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
The data set dta is Jeff's, it's in dput format.
Hope this helps,
Rui Barradas
?s 18:26 de 02/08/2020, Rasmus Liland escreveu:
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
______________________________________________ R-help at 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.
Este e-mail foi verificado em termos de v?rus pelo software antiv?rus Avast. https://www.avast.com/antivirus [[alternative HTML version deleted]]