gpt4 book ai didi

r - 为什么所有日期字符串都变成数字?

转载 作者:行者123 更新时间:2023-12-01 09:59:07 26 4
gpt4 key购买 nike

start=as.Date("2013-09-02")
x[1:140]<-start
for(i in 2:140){
x[i]<-x[i-1]+1 }

我得到的是这样的:

[1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"    

当我运行时:

y<-matrix(x,nrow=20,byrow=TRUE) 
> y

[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 15950 15951 15952 15953 15954 15955 15956

所有的字符串都变成了数字,为什么?我怎么能不在y中改变它们呢?

最佳答案

问题是 R 不允许用日期填充矩阵。两者 "matrix""Date"是类。 R 中 Date 对象的底层表示只是一个整数。因此,当您创建矩阵时 y ,它从 x 中获取基础数据(这是一个整数数组),添加一个维度属性,并使类 "matrix" .

没有任何干净的方法可以解决这个问题。不过,您可以使用一些技巧。例如,您可以显式强制 y上课"Date"以及"matrix"class(y)<-c("matrix","Date") . y仍然会打印出日期向量,但您可以使用矩阵坐标对其进行操作:

> class(y)<-c("matrix","Date")
> head(y)
[1] "2013-09-02" "2013-09-09" "2013-09-16" "2013-09-23" "2013-09-30"
[6] "2013-10-07" "2013-09-03" "2013-09-10" "2013-09-17" "2013-09-24"
[11] "2013-10-01" "2013-10-08" "2013-09-04" "2013-09-11" "2013-09-18"
[16] "2013-09-25" "2013-10-02" "2013-10-09" "2013-09-05" "2013-09-12"
[21] "2013-09-19" "2013-09-26" "2013-10-03" "2013-10-10" "2013-09-06"
[26] "2013-09-13" "2013-09-20" "2013-09-27" "2013-10-04" "2013-10-11"
[31] "2013-09-07" "2013-09-14" "2013-09-21" "2013-09-28" "2013-10-05"
[36] "2013-10-12" "2013-09-08" "2013-09-15" "2013-09-22" "2013-09-29"
[41] "2013-10-06" "2013-10-13"
> y[1,]
[1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[6] "2013-09-07" "2013-09-08"
> y[1,]<-y[1,]+1
> y[1,]
[1] "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" "2013-09-07"
[6] "2013-09-08" "2013-09-09"

您也可以使用数据框代替矩阵:

> y<-data.frame(y)
> y<-data.frame(lapply(y,function(x) {class(x)<-"Date";x}))
> head(y)
X1 X2 X3 X4 X5 X6 X7
1 2013-09-02 2013-09-03 2013-09-04 2013-09-05 2013-09-06 2013-09-07 2013-09-08
2 2013-09-09 2013-09-10 2013-09-11 2013-09-12 2013-09-13 2013-09-14 2013-09-15
3 2013-09-16 2013-09-17 2013-09-18 2013-09-19 2013-09-20 2013-09-21 2013-09-22
4 2013-09-23 2013-09-24 2013-09-25 2013-09-26 2013-09-27 2013-09-28 2013-09-29
5 2013-09-30 2013-10-01 2013-10-02 2013-10-03 2013-10-04 2013-10-05 2013-10-06
6 2013-10-07 2013-10-08 2013-10-09 2013-10-10 2013-10-11 2013-10-12 2013-10-13

第三种可能性是将元素保留在 y 中以字符格式,然后使用 as.Date 将它们转换为日期当您需要它们时:

> y<-matrix(as.character(x),nrow=20,byrow=T)
> head(y)
[,1] [,2] [,3] [,4] [,5]
[1,] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[2,] "2013-09-09" "2013-09-10" "2013-09-11" "2013-09-12" "2013-09-13"
[3,] "2013-09-16" "2013-09-17" "2013-09-18" "2013-09-19" "2013-09-20"
[4,] "2013-09-23" "2013-09-24" "2013-09-25" "2013-09-26" "2013-09-27"
[5,] "2013-09-30" "2013-10-01" "2013-10-02" "2013-10-03" "2013-10-04"
[6,] "2013-10-07" "2013-10-08" "2013-10-09" "2013-10-10" "2013-10-11"
[,6] [,7]
[1,] "2013-09-07" "2013-09-08"
[2,] "2013-09-14" "2013-09-15"
[3,] "2013-09-21" "2013-09-22"
[4,] "2013-09-28" "2013-09-29"
[5,] "2013-10-05" "2013-10-06"
[6,] "2013-10-12" "2013-10-13"
> as.Date(y[1,])
[1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[6] "2013-09-07" "2013-09-08"

关于r - 为什么所有日期字符串都变成数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19225683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com