使用read.zoo代替read.table和zoo()?
我有一个包含许多此类行的文件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
f
中的拼写错误已经被指出。read.zoo
的一些功能。首先,请注意,如果index
参数的值是一个列表,则该列表的每个组件中引用的列将作为单独的参数传递给FUN
。另请注意,可以使用FUN2
参数,该参数应用于FUN
的输出,因此我们可以以紧凑的方式编写它,如下所示:因此尝试以下操作:
上面是这样写的是独立的,因此您可以将其逐字复制到剪贴板,然后将其粘贴到 R 会话中。要将其与您的文件一起使用,请将
textConnection(Lines)
替换为"myfile"
。f
was already pointed out.read.zoo
that you might wish to take advantage of. Firstly, note that if the value of theindex
argument is a list then the columns referenced in each component of that list are passed as separate arguments toFUN
. Also note that aFUN2
argument is available which is applied to the output ofFUN
so we can write it in a compact fashion like this:Thus try this:
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"
.您的函数
f
必须搜索tmp
。您可能想要:此外,您发布的示例数据看起来是制表符分隔的,而不是空格分隔的,因此您可能需要这样做:
Your function
f
has to search fortmp
. You probably intended:Also, the sample data you posted looks like it's tab-delimited, not space-delimited, so you may need to this instead: