将xts对象转换为有用的数据框

时间:2013-06-06 13:18:17

标签: r dataframe xts

名为d的数据框包含以下数据:

timestamp,value
"2013-06-02 00:00:00",70
"2013-06-02 00:02:00",70
"2013-06-02 00:07:00",60
"2013-06-02 00:15:00",70
"2013-06-02 00:12:00",60
"2013-06-02 00:30:00",70
"2013-06-02 00:45:00",70
"2013-06-02 01:00:00",70

我的代码是:

 d = read.csv(path, header=TRUE, sep=",")
 d2 <- xts(x = d[c("value")], order.by = as.POSIXct(d[, "timestamp"], tz = "GMT", format = "%Y-%m-%d %H:%M:%S"))
ends <- endpoints(d2, on = "minutes", k = 15)
d3   <- period.apply(d2, ends, mean)

之后我想将xts对象转换为数据帧,我正在使用它:

d3$timestamp = rownames(d3)
rownames(d3) = NULL
d3$timestamp = strptime(d3$timestamp, "%Y-%m-%d %H:%M:%S")

然而,在最后一步中,它会向此输出错误:

Error in NextMethod(.Generic) : 
   number of items to replace is not a multiple of replacement length

当我观察整个命令后输入d3时,对象具有这种格式的数据:

                         timestamp
2013-06-02 00:15:00        65
2013-06-02 00:30:00        70
2013-06-02 00:45:00        70
2013-06-02 01:00:00        70

但是,在列名中,它必须具有名称值,而第二列的时间戳必须为​​here。可能有什么不对?

正确的输出必须是:

      value
        65  2013-06-02 00:15:00
        70  2013-06-02 00:30:00
        70  2013-06-02 00:45:00
        70  2013-06-02 01:00:00 

2 个答案:

答案 0 :(得分:9)

您可以像这样创建data.frame:

 data.frame(value=coredata(d3),timestamp=index(d3))
# value           timestamp
# 1    65 2013-06-02 00:12:00
# 2    70 2013-06-02 00:15:00
# 3    70 2013-06-02 00:30:00
# 4    70 2013-06-02 00:45:00
# 5    70 2013-06-02 01:00:00

我建议您使用read.zoo将您的数据作为动物园对象读取,并避免手动强制xts。例如:

dat <- read.zoo(text='timestamp,value
"2013-06-02 00:00:00",70
"2013-06-02 00:02:00",70
"2013-06-02 00:07:00",60
"2013-06-02 00:15:00",70
"2013-06-02 00:12:00",60
"2013-06-02 00:30:00",70
"2013-06-02 00:45:00",70
"2013-06-02 01:00:00",70',tz ='' , format = "%Y-%m-%d %H:%M:%S",header=TRUE,
         sep=',')
d2 <- as.xts(dat)

答案 1 :(得分:1)

另一个选项是tidyquant包,它作为两个函数用于向xts对象强制转换(转换)数据帧:即as_xts()用于将数据帧转换为xts,以及as_tibble()用于将xts(和其他时间序列或矩阵对象)转换为“整洁”的数据帧。

这是一个简单的例子。我使用tribble()函数重新创建您的示例。在转换过程中,我使用as_datetime()中的lubridate函数(tidyquant自动加载此函数)将字符转换为日期时间类。其他一切都应该非常简单。


library(tidyquant)

# Recreate data frame
data_df <- tribble(
    ~timestamp, ~value,
    "2013-06-02 00:00:00", 70,
    "2013-06-02 00:02:00", 70,
    "2013-06-02 00:07:00", 60,
    "2013-06-02 00:15:00", 70,
    "2013-06-02 00:12:00", 60,
    "2013-06-02 00:30:00", 70,
    "2013-06-02 00:45:00", 70,
    "2013-06-02 01:00:00", 70
)
data_df
#> # A tibble: 8 × 2
#>             timestamp value
#>                 <chr> <dbl>
#> 1 2013-06-02 00:00:00    70
#> 2 2013-06-02 00:02:00    70
#> 3 2013-06-02 00:07:00    60
#> 4 2013-06-02 00:15:00    70
#> 5 2013-06-02 00:12:00    60
#> 6 2013-06-02 00:30:00    70
#> 7 2013-06-02 00:45:00    70
#> 8 2013-06-02 01:00:00    70

# Convert data frame to xts
data_xts <- data_df %>%
    mutate(timestamp = as_datetime(timestamp, tz = Sys.timezone())) %>%
    as_xts(date_col = timestamp)
data_xts
#>                     value
#> 2013-06-02 00:00:00    70
#> 2013-06-02 00:02:00    70
#> 2013-06-02 00:07:00    60
#> 2013-06-02 00:12:00    60
#> 2013-06-02 00:15:00    70
#> 2013-06-02 00:30:00    70
#> 2013-06-02 00:45:00    70
#> 2013-06-02 01:00:00    70

# Convert xts to data frame
data_df_2 <- data_xts %>%
    as_tibble(preserve_row_names = TRUE) %>%
    rename(timestamp = row.names) %>%
    mutate(timestamp = as_datetime(timestamp, tz = Sys.timezone()))
data_df_2
#> # A tibble: 8 × 2
#>             timestamp value
#>                <dttm> <dbl>
#> 1 2013-06-02 00:00:00    70
#> 2 2013-06-02 00:02:00    70
#> 3 2013-06-02 00:07:00    60
#> 4 2013-06-02 00:12:00    60
#> 5 2013-06-02 00:15:00    70
#> 6 2013-06-02 00:30:00    70
#> 7 2013-06-02 00:45:00    70
#> 8 2013-06-02 01:00:00    70
相关问题