Zoo merge() 和合并的列名

发布于 2024-12-31 21:56:04 字数 603 浏览 1 评论 0原文

我对 R 比较陌生。我正在将多个 csv 文件中包含的数据合并到单个动物园对象中。

以下是我的 for 循环 中的代码片段:

temp <- read.csv(filename, stringsAsFactors=F)
temp_dates <- as.Date(temp[,2])
temp <- zoo(temp[,17], temp_dates)
dataset <- temp[seq_specified_dates]

# merge data into output
if (length(output) == 0)
    output <- dataset
else
    output <- merge(output, dataset, all=FALSE)

当我在输出动物园对象上运行 head() 时,我注意到奇怪的命名列名称,例如:“dataset.output.output.output”等。如何为合并的列分配更有意义的名称。 ?

另外,如何引用动物园对象中的特定列?例如,如果输出是数据帧,我可以将“Patient_A”列引用为输出$Patient_A。如何引用合并的动物园对象中的特定列?

I am relatively new to R. I am merging data contained in multiple csv files into a single zoo object.

Here is a snippet of the code in my for loop:

temp <- read.csv(filename, stringsAsFactors=F)
temp_dates <- as.Date(temp[,2])
temp <- zoo(temp[,17], temp_dates)
dataset <- temp[seq_specified_dates]

# merge data into output
if (length(output) == 0)
    output <- dataset
else
    output <- merge(output, dataset, all=FALSE)

When I run head() on the output zoo object, I notice bizarrely named column names like: 'dataset.output.output.output' etc. How can I assign more meaningful names to the merged columns. ?

Also, how do I reference a particular column in a zoo object?. For example if output was a dataframe, I could reference the 'Patient_A' column as output$Patient_A. How do I reference a specific column in a merged zoo object?

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

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

发布评论

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

评论(2

旧时浪漫 2025-01-07 21:56:04

我认为无论动物园课程的日期如何,这都会起作用,如果您提供一个示例,我也许能够修复细节,但总而言之,这应该是一个很好的起点。

#1- Put your multiple csv files in one folder
setwd(your path)
listnames = list.files(pattern=".csv")

#2-use package plyr
library(plyr)

    pp1 = ldply(listnames,read.csv,header=T) #put all the files in on data.frame
    names(pp1)=c('name1','name2','name3',...)
    pp1$date =  zoo(pp1$date)


# Reshape data frame so it gets organized by date
pp2=reshape(pp1,timevar='name1',idvar='date',direction='wide')

I think this would work regardless of the date being a zoo class, if you provide an example I may be able to fix the details, but all in all this should be a good starting point.

#1- Put your multiple csv files in one folder
setwd(your path)
listnames = list.files(pattern=".csv")

#2-use package plyr
library(plyr)

    pp1 = ldply(listnames,read.csv,header=T) #put all the files in on data.frame
    names(pp1)=c('name1','name2','name3',...)
    pp1$date =  zoo(pp1$date)


# Reshape data frame so it gets organized by date
pp2=reshape(pp1,timevar='name1',idvar='date',direction='wide')
只是我以为 2025-01-07 21:56:04

read.zoo 能够读取和合并多个文件。例如:

idx <- seq(as.Date('2012-01-01'),  by = 'day',  length = 30)

dat1<- data.frame(date = idx,  x = rnorm(30))
dat2<- data.frame(date = idx,  x = rnorm(30))
dat3<- data.frame(date = idx,  x = rnorm(30))

write.table(dat1,  file = 'ex1.csv')
write.table(dat2,  file = 'ex2.csv')
write.table(dat3,  file = 'ex3.csv')

datMerged <- read.zoo(c('ex1.csv',  'ex2.csv',  'ex3.csv'))

如果您想访问特定列,您可以使用 $ 方法:

datMerged$ex1.csv

编辑:
您可以使用window方法提取时间段:

window(datMerged, start='2012-01-28', end='2012-01-30')

xts包包含更多提取方法:

library(xts)
datMergedx['2012-01-03']
datMergedx['2012-01-28/2012-01-30']

read.zoo is able to read and merge multiple files. For example:

idx <- seq(as.Date('2012-01-01'),  by = 'day',  length = 30)

dat1<- data.frame(date = idx,  x = rnorm(30))
dat2<- data.frame(date = idx,  x = rnorm(30))
dat3<- data.frame(date = idx,  x = rnorm(30))

write.table(dat1,  file = 'ex1.csv')
write.table(dat2,  file = 'ex2.csv')
write.table(dat3,  file = 'ex3.csv')

datMerged <- read.zoo(c('ex1.csv',  'ex2.csv',  'ex3.csv'))

If you want to access a particular column you can use the $ method:

datMerged$ex1.csv

EDITED:
You can extract a time period with the window method:

window(datMerged, start='2012-01-28', end='2012-01-30')

The xts package includes more extraction methods:

library(xts)
datMergedx['2012-01-03']
datMergedx['2012-01-28/2012-01-30']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文