[R] Parsing a Date

Rasmus Liland jr@| @end|ng |rom po@teo@no
Sun Aug 2 19:26:48 CEST 2020


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200802/465df8ed/attachment.sig>


More information about the R-help mailing list