xts 删除列名

xts dropping column names

我有一个data.frame

res0 = structure(list(year = "2017", il = 11200000), .Names = c("year", 
"il"), row.names = c(NA, -1L), class = "data.frame")

但是,当我尝试将其设为 xts 对象时,我丢失了列名。

as.xts(x = res0[,2:ncol(res0)], order.by = as.POSIXct(paste0(res0$year,"-01-01")), name = NULL)

这个returns:

               [,1]
2017-01-01 11200000 

而不是

                 il
2017-01-01 11200000 

R 中的下标默认会降低维度。使用 drop = FALSE 来防止这种情况。

res0[, 2:ncol(res0), drop = FALSE]

另请注意,这可以创建一个以年份为索引的 n x 1 动物园系列。

library(zoo)
z <- read.zoo(res0, FUN = c, drop = FALSE)