簡體   English   中英

將xts對象轉換為有用的數據框

[英]Converting an xts object to a useful data frame

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

您可以像這樣創建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)

另一種選擇是tidyquant包,它作為兩個函數用於強制(轉換)數據幀與xts對象之間的轉換:即as_xts()用於將數據幀轉換為xts, as_tibble()用於轉換xts(以及其他時間序列或矩陣對象) )“整理”數據幀。

這是一個簡單的例子。 我使用tribble()函數重新創建您的示例。 在轉換過程中,我用的是as_datetime()從功能lubridatetidyquant此自動負載)從字符轉換日期時間類。 其他一切都應該非常簡單。


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM