从 R 中的 CSV 文件读取 xts

发布于 2024-09-25 17:11:49 字数 627 浏览 5 评论 0原文

我正在尝试从 CSV 文件读取时间序列并将它们保存为 xts 以便能够使用 quantmod 处理它们。问题是数值没有被解析。

CSV 文件:

name;amount;datetime
test1;3;2010-09-23 19:00:00.057
test2;9;2010-09-23 19:00:00.073

R 代码:

library(xts)
ColClasses = c("character", "numeric", "character")
Data <- read.zoo("c:\\dat\\test2.csv", index.column = 3, sep = ";", header = TRUE, FUN = as.POSIXct, colClasses = ColClasses)
as.xts(Data)

结果:

                    name    amount
2010-09-23 19:00:00 "test1" "3"   
2010-09-23 19:00:00 "test2" "9"   

查看金额列包含字符数据,但应为数字。我的代码有什么问题吗?

I'm trying to read time series from CSV file and save them as xts to be able to process them with quantmod. The problem is that numeric values are not parsed.

CSV file:

name;amount;datetime
test1;3;2010-09-23 19:00:00.057
test2;9;2010-09-23 19:00:00.073

R code:

library(xts)
ColClasses = c("character", "numeric", "character")
Data <- read.zoo("c:\\dat\\test2.csv", index.column = 3, sep = ";", header = TRUE, FUN = as.POSIXct, colClasses = ColClasses)
as.xts(Data)

Result:

                    name    amount
2010-09-23 19:00:00 "test1" "3"   
2010-09-23 19:00:00 "test2" "9"   

See amount column contains character data but expected to be numeric. What's wrong with my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久光 2024-10-02 17:11:49

zooxts 的内部数据结构都是matrix,因此不能混合数据类型。


只需使用 read.table 读取数据:

Data <- read.table("file.csv", sep=";", header=TRUE, colClasses=ColClasses)

我注意到您的数据有亚秒级,因此您可能对 xts::align.time 感兴趣。此代码将获取数据并按秒为每个“name”创建一个带有一列的对象。

NewData <- do.call( merge, lapply( split(Data,Data$name), function(x) {
  align.time( xts(x[,"amount"],as.POSIXct(x[,"datetime"])), n=1 )
}) )

如果您想在全局环境中创建对象 test1test2,您可以执行以下操作:

lapply( split(Data,Data$name), function(x) {
  assign(x[,"name"], xts(x[,"amount"],as.POSIXct(x[,"datetime"])),envir=.GlobalEnv)
})

The internal data structure of both zoo and xts is matrix, so you cannot mix data types.


Just read in the data with read.table:

Data <- read.table("file.csv", sep=";", header=TRUE, colClasses=ColClasses)

I notice your data have subseconds, so you may be interested in xts::align.time. This code will take Data and create one object with a column for each "name" by seconds.

NewData <- do.call( merge, lapply( split(Data,Data$name), function(x) {
  align.time( xts(x[,"amount"],as.POSIXct(x[,"datetime"])), n=1 )
}) )

If you want to create objects test1 and test2 in your global environment, you can do something like:

lapply( split(Data,Data$name), function(x) {
  assign(x[,"name"], xts(x[,"amount"],as.POSIXct(x[,"datetime"])),envir=.GlobalEnv)
})
溺深海 2024-10-02 17:11:49

您不能在 Zoo 或 xts 对象中混合数字和字符数据;但是,如果 name 列不是为了作为时间序列数据,而是为了区分多个时间序列,一个用于 test1,一个用于 test2 等,那么您可以使用 split=1 在第 1 列上进行拆分以进行此类拆分如下面的代码所示。请务必设置digits.secs,否则您将不会在输出中看到亚秒(尽管它们在任何情况下都会存在):

options(digits.secs = 3)
z <- read.zoo("myfile.csv", sep = ";", split = 1, index = 3, header = TRUE, tz = "")
x <- as.xts(z)

You cannot mix numeric and character data in a zoo or xts object; however, if the name column is not intended to be time series data but rather is intended to distinguish between multiple time series, one for test1, one for test2, etc. then you can split on column 1 using split=1 to cause such splitting as shown in the following code. Be sure to set the digits.secs or else you won't see the sub-seconds on output (although they will be there in any case):

options(digits.secs = 3)
z <- read.zoo("myfile.csv", sep = ";", split = 1, index = 3, header = TRUE, tz = "")
x <- as.xts(z)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文