xts:如何在as.xts之后控制数据类型?

时间:2016-10-07 14:52:46

标签: r xts lubridate

考虑以下数据框

time <-c('2016-04-13 23:07:45','2016-04-13 23:07:55','2016-04-13 23:08:45','2016-04-13 23:08:45'
         ,'2016-04-13 23:08:45','2016-04-13 23:07:50','2016-04-13 23:07:51')
group <-c('A','A','A','B','B','B','B')
value<- c(5,10,2,2,NA,1,4)
df=data.frame(time,group,value)

> df
                 time group value
1 2016-04-13 23:07:45     A     5
2 2016-04-13 23:07:55     A    10
3 2016-04-13 23:08:45     A     2
4 2016-04-13 23:08:45     B     2
5 2016-04-13 23:08:45     B    NA
6 2016-04-13 23:07:50     B     1
7 2016-04-13 23:07:51     B     4

请注意缺失值行5。现在,在使用xts将我的时间戳转换为正确的Posix类型后,我转换为lubridate

> df$time = ymd_hms(df$time)
> df<-as.xts(df,order.by=df$time)
> df
                    time                  group value
2016-04-13 23:07:45 "2016-04-13 23:07:45" "A"   " 5" 
2016-04-13 23:07:50 "2016-04-13 23:07:50" "B"   " 1" 
2016-04-13 23:07:51 "2016-04-13 23:07:51" "B"   " 4" 
2016-04-13 23:07:55 "2016-04-13 23:07:55" "A"   "10" 
2016-04-13 23:08:45 "2016-04-13 23:08:45" "A"   " 2" 
2016-04-13 23:08:45 "2016-04-13 23:08:45" "B"   " 2" 
2016-04-13 23:08:45 "2016-04-13 23:08:45" "B"   NA   

我好numericvalue现在是character

我该如何避免?

谢谢!

1 个答案:

答案 0 :(得分:3)

数据的基础xts对象是一个矩阵,可以是数字或字符类型,但不能同时是两者(与data.frame不同,data.frame是一个列表,其中每列可以是R中的任何原子类型)。看到这种情况的粗略检查是试试这个:

> as.matrix(df)
     time                  group value
[1,] "2016-04-13 23:07:45" "A"   " 5" 
[2,] "2016-04-13 23:07:55" "A"   "10" 
[3,] "2016-04-13 23:08:45" "A"   " 2" 
[4,] "2016-04-13 23:08:45" "B"   " 2" 
[5,] "2016-04-13 23:08:45" "B"   NA   
[6,] "2016-04-13 23:07:50" "B"   " 1" 
[7,] "2016-04-13 23:07:51" "B"   " 4"

创建coredata对象时xts返回的内容:

x.df<- xts(df,order.by=df$time)
> coredata(x.df)
     time                  group value
[1,] "2016-04-13 23:07:45" "A"   " 5" 
[2,] "2016-04-13 23:07:50" "B"   " 1" 
[3,] "2016-04-13 23:07:51" "B"   " 4" 
[4,] "2016-04-13 23:07:55" "A"   "10" 
[5,] "2016-04-13 23:08:45" "A"   " 2" 
[6,] "2016-04-13 23:08:45" "B"   " 2" 
[7,] "2016-04-13 23:08:45" "B"   NA   

在创建xts对象时删除timegroup列,以获得您期望的数值数据。您可以将组列类型映射到整数。您还不应在time参数的xts对象创建中包含x,因为您的order.by已包含时间信息。

e.g。

df$group_idx <- as.numeric(as.factor(df$group))
x.df<- xts(df[, c("group_idx", "value")],order.by=df$time)
> x.df
                    group_idx value
2016-04-13 23:07:45         1     5
2016-04-13 23:07:50         2     1
2016-04-13 23:07:51         2     4
2016-04-13 23:07:55         1    10
2016-04-13 23:08:45         1     2
2016-04-13 23:08:45         2     2
2016-04-13 23:08:45         2    NA