使用read.zoo代替read.table和zoo()?

发布于 2024-10-03 16:01:26 字数 785 浏览 3 评论 0原文

我有一个包含许多此类行的文件

2010-01-12 19:40   1021.00000   0.00001     1.00
2010-01-12 19:50   1031.00000   0.00000     -1.00

为了将其读取为动物园,我使用

tmp <- read.table("myfile")
GOEMD <- zoo(tmp[,3], as.chron(paste(tmp[,1],tmp[,2]), format="%Y-%m-%d %H:%M"))

它可以正常工作 但我想使用 read.zoo() 代替。

我尝试过

f <- function(x)  as.chron(paste(tmp[,1],tmp[,2]))
tmp <- read.zoo("myfile", index = 1:2, sep=" ", FUN  = f)

,甚至指定过

colClasses= c("character","character","numeric","numeric","numeric")

,但它不起作用; 它说: 第 136 行(我在上面粘贴的那一行)没有 14 个元素。

我也尝试过:

tmp <- read.zoo("myfile", index = 1:2, sep=" ", FUN  = as.chron)

I have a file with many rows of this kind

2010-01-12 19:40   1021.00000   0.00001     1.00
2010-01-12 19:50   1031.00000   0.00000     -1.00

In order to read it as zoo I use

tmp <- read.table("myfile")
GOEMD <- zoo(tmp[,3], as.chron(paste(tmp[,1],tmp[,2]), format="%Y-%m-%d %H:%M"))

that works properly
But I'd like to use read.zoo() instead.

I've tried

f <- function(x)  as.chron(paste(tmp[,1],tmp[,2]))
tmp <- read.zoo("myfile", index = 1:2, sep=" ", FUN  = f)

and even specifying

colClasses= c("character","character","numeric","numeric","numeric")

but it doesn't work;
it says:
line 136 (the one I've pasted above) doesn't have 14 elements.

I've also tried:

tmp <- read.zoo("myfile", index = 1:2, sep=" ", FUN  = as.chron)

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

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

发布评论

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

评论(2

风吹过旳痕迹 2024-10-10 16:01:26
  1. f 中的拼写错误已经被指出。
  2. 此外,您可能希望利用 read.zoo 的一些功能。首先,请注意,如果 index 参数的值是一个列表,则该列表的每个组件中引用的列将作为单独的参数传递给 FUN。另请注意,可以使用 FUN2 参数,该参数应用于 FUN 的输出,因此我们可以以紧凑的方式编写它,如下所示:

因此尝试以下操作:

library(zoo)
library(chron)

Lines <- "2010-01-12 19:40   1021.00000   0.00001     1.00
2010-01-12 19:50   1031.00000   0.00000     -1.00"

z <- read.zoo(textConnection(Lines), index = list(1, 2), 
        FUN = paste, FUN2 = as.chron)

上面是这样写的是独立的,因此您可以将其逐字复制到剪贴板,然后将其粘贴到 R 会话中。要将其与您的文件一起使用,请将 textConnection(Lines) 替换为 "myfile"

  1. The typo in f was already pointed out.
  2. Also there are a few features of read.zoo that you might wish to take advantage of. Firstly, note that if the value of the index argument is a list then the columns referenced in each component of that list are passed as separate arguments to FUN. Also note that a FUN2 argument is available which is applied to the output of FUN so we can write it in a compact fashion like this:

Thus try this:

library(zoo)
library(chron)

Lines <- "2010-01-12 19:40   1021.00000   0.00001     1.00
2010-01-12 19:50   1031.00000   0.00000     -1.00"

z <- read.zoo(textConnection(Lines), index = list(1, 2), 
        FUN = paste, FUN2 = as.chron)

The above was written to be self contained so you could just copy it verbatim to the clipboard and then paste it into your R session. To use it with your file replace textConnection(Lines) with "myfile".

醉生梦死 2024-10-10 16:01:26

您的函数 f 必须搜索 tmp。您可能想要:

f <- function(x)  as.chron(paste(x[,1],x[,2]))
tmp <- read.zoo("myfile", index = 1:2, sep=" ", FUN = f)

此外,您发布的示例数据看起来是制表符分隔的,而不是空格分隔的,因此您可能需要这样做:

tmp <- read.zoo("myfile", index = 1, sep="\t", FUN = as.chron)

Your function f has to search for tmp. You probably intended:

f <- function(x)  as.chron(paste(x[,1],x[,2]))
tmp <- read.zoo("myfile", index = 1:2, sep=" ", FUN = f)

Also, the sample data you posted looks like it's tab-delimited, not space-delimited, so you may need to this instead:

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