[英]Is there any way to store date and time data in an array in R?
I understand that data.frame is of dimension 2 and array is like a matrix in higher dimensions so its elements should of the same data type. 我知道data.frame是2维的,而array就像是更高维度的矩阵,因此它的元素应该是相同的数据类型。 I wonder if there is any class or solution to store data including both date and time and numeric values to a variable of dimension 3. Thank you for your help. 我想知道是否存在任何类或解决方案来将数据(包括日期和时间以及数字值)存储到维度3的变量中。谢谢您的帮助。
All elements in an array must be of the same data type. 数组中的所有元素必须具有相同的数据类型。 However; 然而; you can convert your date and time to "number of seconds since epoch" and use that since it is numeric. 您可以将日期和时间转换为“距纪元的秒数”,并使用数字形式的值。 For example how to convert the current time: 例如如何转换当前时间:
>st <- Sys.Time()
>print(st)
[1] "2018-03-30 23:55:56 CDT"
>print(as.numeric(st))
[1] 1522472156
A "POSIXct"
vector such as x
below can be given dimensions using dim<-
阿"POSIXct"
载体如x
下面可以给出使用维dim<-
x <- Sys.time() + 1:8
dim(x) <- c(2, 2, 2)
class(x)
## [1] "POSIXct" "POSIXt"
dim(x)
## [1] 2 2 2
It can alternately be written in this form: 也可以用以下形式编写:
x <- Sys.time() + 1:8
xx <- `dim<-`(x, c(2, 2, 2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.