5

名为 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

但是,在列名中,它必须具有名称值,并且第二列具有像这里一样的时间戳。有什么问题?

正确的输出必须是这样的:

      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 
4

2 回答 2

10

您可以像这样创建 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)
于 2013-06-06T13:41:35.720 回答
2

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

这是一个简单的例子。我使用该tribble()功能重新创建您的示例。在转换过程中,我使用as_datetime()函数 from 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
于 2017-03-05T13:42:52.503 回答