简体   繁体   English

xts删除列名称

[英]xts dropping column names

I have a data.frame 我有一个data.frame

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

however, when I try to make this an xts object I lose the column names. 但是,当我尝试使它成为xts对象时,我丢失了列名。

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

This returns: 返回:

               [,1]
2017-01-01 11200000 

instead of 代替

                 il
2017-01-01 11200000 

Subscripting in R drops dimensions by default. 默认情况下,R下标会删除尺寸。 Use drop = FALSE to prevent this. 使用drop = FALSE可以防止这种情况。

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

Also note that this works to create an nx 1 zoo series with year as the index. 还要注意,这可以创建以年为索引的nx 1动物园系列。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM