gpt4 book ai didi

r - 创建具有多列的 xts 对象

转载 作者:行者123 更新时间:2023-12-04 13:49:57 24 4
gpt4 key购买 nike

我在一个文件夹中有多个文件,我通过以下方式读取这些文件:

files <- list.files( "PATH", pattern = '*csv' , full.names = TRUE)
(length(files))
for( i in length(files) ) {
df <- fread(files[i], header = TRUE, sep = ";",stringsAsFactors=FALSE)
}

我知道我正在一次又一次地覆盖 df 对象。我想要实现的是:

我想将我的数据格式化为这个示例数据:

> (str(StockPriceReturns))
'zoo' series from 2000-04-03 to 2013-03-28
Data: num [1:3246, 1:30] NA NA NA NA NA NA NA NA NA NA ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:30] "Bajaj.Auto" "BHEL" "Bharti.Airtel" "Cipla" ...
Index: Date[1:3246], format: "2000-04-03" "2000-04-04" "2000-04-05" "2000-04-06" ...
NULL
> (head(StockPriceReturns))
Bajaj.Auto BHEL Bharti.Airtel Cipla Coal.India Dr.Reddy
2000-04-03 NA 4.9171044 NA 6.810041 NA -3.2541653
2000-04-04 NA -8.3348496 NA -3.368606 NA -8.3353739
2000-04-05 NA 0.3305788 NA 0.836825 NA 0.2616345
2000-04-06 NA -2.7605266 NA -2.466056 NA -1.8941289
2000-04-07 NA 3.2543548 NA 7.690426 NA 7.6961041
2000-04-10 NA 3.3107586 NA 6.154276 NA 6.4769648

目前我的数据实际上是这样的:

> (dput(head(df1,10)))
structure(list(Name = c("C-QUADRAT INVESTMENT", "C-QUADRAT INVESTMENT",
"C-QUADRAT INVESTMENT", "C-QUADRAT INVESTMENT", "C-QUADRAT INVESTMENT",
"C-QUADRAT INVESTMENT", "C-QUADRAT INVESTMENT", "C-QUADRAT INVESTMENT",
"C-QUADRAT INVESTMENT", "C-QUADRAT INVESTMENT"), Date = c("01.01.2002",
"02.01.2002", "03.01.2002", "04.01.2002", "07.01.2002", "08.01.2002",
"09.01.2002", "10.01.2002", "11.01.2002", "14.01.2002"), Price = c("na",
"na", "na", "na", "na", "na", "na", "na", "na", "na"), Currency = c("E",
"E", "E", "E", "E", "E", "E", "E", "E", "E"), CDax = c("-0,260460226",
"-1,827437365", "-0,814370143", "0,861279951", "-0,339133689",
"-1,034650372", "0,713336597", "0,52727784", "2,893518519", "0,05790388"
), `Total Price Returns` = c("na", "na", "na", "na", "na", "na",
"na", "na", "na", "na"), AbnormalReturns = c("na", "na", "na",
"na", "na", "na", "na", "na", "na", "na")), .Names = c("Name",
"Date", "Price", "Currency", "CDax", "Total Price Returns", "AbnormalReturns"
), class = c("data.table", "data.frame"), row.names = c(NA, -10L
), .internal.selfref = <pointer: 0x00000000002a0788>)
Name Date Price Currency CDax
1: C-QUADRAT INVESTMENT 01.01.2002 na E -0,260460226
2: C-QUADRAT INVESTMENT 02.01.2002 na E -1,827437365
3: C-QUADRAT INVESTMENT 03.01.2002 na E -0,814370143
4: C-QUADRAT INVESTMENT 04.01.2002 na E 0,861279951
5: C-QUADRAT INVESTMENT 07.01.2002 na E -0,339133689
6: C-QUADRAT INVESTMENT 08.01.2002 na E -1,034650372
7: C-QUADRAT INVESTMENT 09.01.2002 na E 0,713336597
8: C-QUADRAT INVESTMENT 10.01.2002 na E 0,52727784
9: C-QUADRAT INVESTMENT 11.01.2002 na E 2,893518519
10: C-QUADRAT INVESTMENT 14.01.2002 na E 0,05790388
Total Price Returns AbnormalReturns
1: na na
2: na na
3: na na
4: na na
5: na na
6: na na
7: na na
8: na na
9: na na
10: na na

我可以针对单变量情况执行此操作,我只是将我的 data.frame 转换为 zoo 对象,然后将此对象转换为 xts 具有此功能的对象:

dfToZoo <- function(df) {
require(zoo)
date <- as.Date(df[, 1], format = '%d.%m.%Y')
#TODO have a look if the column are rightly named
with(df, zoo(TotalReturns, date))
}

但是对于多变量情况如何做到这一点?

最佳答案

一种可能性是使用 read.zoo 读取您的数据并使用 split 参数 reshape 它。

# some data
df <- data.frame(Date = 20140101 + 0:2,
Name = rep(c("Bajaj.Auto", "BHEL", "Bharti.Airtel"), each = 3),
CDAX = rnorm(9))
df
# Date Name CDAX
# 1 20140101 Bajaj.Auto 0.4020118
# 2 20140102 Bajaj.Auto -0.7317482
# 3 20140103 Bajaj.Auto 0.8303732
# 4 20140101 BHEL -1.2080828
# 5 20140102 BHEL -1.0479844
# 6 20140103 BHEL 1.4411577
# 7 20140101 Bharti.Airtel -1.0158475
# 8 20140102 Bharti.Airtel 0.4119747
# 9 20140103 Bharti.Airtel -0.3810761

# convert to zoo object. Use 'split' to reshape, and 'format' date if necessary.
z <- read.zoo(file = df, format = "%Y%m%d", split = "Name")

str(z)
# ‘zoo’ series from 2002-01-01 to 2002-01-04
# Data: num [1:4, 1:3] 2.308 0.1058 0.457 -0.0772 1.0274 ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:3] "Bajaj.Auto" "Bharti.Airtel" "BHEL"
# Index: Date[1:4], format: "2002-01-01" "2002-01-02" "2002-01-03" "2002-01-04"

z
# Bajaj.Auto Bharti.Airtel BHEL
# 2014-01-01 0.4020118 -1.0158475 -1.208083
# 2014-01-02 -0.7317482 0.4119747 -1.047984
# 2014-01-03 0.8303732 -0.3810761 1.441158

关于r - 创建具有多列的 xts 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20937211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com