不同文件之间的相关矩阵 [英] Correlation matrix between different files
问题描述
我有82个.csv文件,每个文件都是一个Zoo对象,格式如下:
"Index", "code", "pp"
1951-01-01, 2030, 22.9
1951-01-02, 2030, 0.5
1951-01-03, 2030, 0.0
I have 82 .csv files, each of them a zoo object, with the following format:
"Index", "code", "pp"
1951-01-01, 2030, 22.9
1951-01-02, 2030, 0.5
1951-01-03, 2030, 0.0
我想在所有文件的pp之间建立一个相关矩阵.我发现了如何在两个文件之间手动"执行操作:
zz<-merge(x,y, all = FALSE)
z<-cbind(zz[,2], zz[,4])
cor(z,use= "complete.obs")
I want to do a correlation matrix between the pp of all of my files. I found out how to do it "manually" between two files:
zz<-merge(x,y, all = FALSE)
z<-cbind(zz[,2], zz[,4])
cor(z,use= "complete.obs")
但是我无法为所有文件提供一个循环来做...要考虑的几件事:每个文件的开始和结束都在不同的日期,我希望矩阵显示代码,以便我可以识别谁是谁.
but I can't come up with a loop to do it for all the files... a few things to consider: each file starts and ends at different dates and I would like the matrix to show the codes so I can identify who is who.
任何人都可以帮忙吗?
推荐答案
实际上,我认为您拥有一个非常好的解决方案.如果以list.files()
开头以生成csv文件列表:
I think you have the bones of a perfectly good solution here, actually. If you start with list.files()
to generate a list of your csv files:
fileList <- list.files(path="path/to/csv/files")
然后使用lapply()
读入所有文件:
then read in all the files using lapply()
:
datList <- lapply(fileList,read.csv)
然后合并前两个文件(假设每个文件的代码相同):
then merge the first two files (assuming the code is the same for each file):
dat <- merge(datList[[1]][,-2],datList[[2]][,-2],by="Index",
suffixes=c(datList[[1]]$code[1],datList[[2]]$code[1]))
suffixes
参数将帮助您按代码命名列,以供将来参考.然后使用简单的for循环遍历datList的其余部分,将每个与dat合并:
The suffixes
argument will help you name the columns by code, for future reference. Then loop over the rest of datList using a simple for loop, merging each one with dat:
for (i in 3:length(datList)){
dat <- merge(dat,datList[[i]][,-2],by="Index",suffixes=datList[[i]]$code[1])
}
,那么您应该能够在dat
减去第一列的情况下运行cor
.您可能需要稍微调整一下此代码,但是这个一般性想法应该可行.
and then you should be able to run cor
on dat
minus the first column. You might have to tweak this code a bit, but this general idea ought to work.
这篇关于不同文件之间的相关矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!