read.csv正在自行修改输入文件名
我试图将目录作为输入传递给函数,并将其作为输入来读取.csv以读取csv文件。但是,在该过程中,read.csv会修改运行时发送的文件名字符串 目录:“C:/SAT/Self-Courses/R/data/specdata” 在这个目录中,有许多CSV文件我想阅读并使用以下函数来执行read.csv正在自行修改输入文件名,r,csv,R,Csv,我试图将目录作为输入传递给函数,并将其作为输入来读取.csv以读取csv文件。但是,在该过程中,read.csv会修改运行时发送的文件名字符串 目录:“C:/SAT/Self-Courses/R/data/specdata” 在这个目录中,有许多CSV文件我想阅读并使用以下函数来执行 complete<-function(directory,id=1:332) { gFull<-c() ids<-str_pad(id,3,pad="0") idExt<-pa
complete<-function(directory,id=1:332)
{
gFull<-c()
ids<-str_pad(id,3,pad="0")
idExt<-paste(ids,".csv",sep="")
dir<-paste(directory,idExt,sep="/")
for(i in dir)
{
tableTemp<- read.csv(i,header=T)
tableTemp<- na.omit(tableTemp)
gFull<-c(gFull,nrow(tableTemp))
}
output<-data.frame(id,gFull,stringsAsFactors = F)
return(output)
}
cor_sub<-function(data,directory)
{
#print(directory)
id<-data[1]
id<-str_pad(id,3,pad="0")
id<-paste(id,".csv",sep="")
#print(id)
dir_temp<-paste(directory,id,sep="/")
print(dir_temp)
#read table
input<-read.csv(dir_temp,header=T)
input<-na.omit(input)
#correlation
return (cor(input$sulfate,input$nitrate))
}
cor<-function(directory,threshold=0)
{
#find the thresholds of each file
qorum<-complete(directory,1:12)
print(threshold)
qorum$gFull[qorum$gFull<threshold]<-NA
qorum<-na.omit(qorum)
v_cor<-apply(qorum,1,cor_sub,directory)
#(v_cor)
}
我得到的错误输出是
> cor("C:/SAT/Self Courses/R/data/specdata",0)
[1] 0
[1] "C:/SAT/Self Courses/R/data/specdata/001.csv"
Show Traceback
Rerun with Debug
Error in file(file, "rt") : cannot open the connection In addition: Warning message:
In file(file, "rt") :
cannot open file '7.21/001.csv': No such file or directory
问题是dir_temp:我有“C:/SAT/Self-Courses/R/data/specdata/001.csv”,但是在下一行read.csv接受输入“7.21/001.csv”
如果这个问题看起来很琐碎,请接受我的提问,我仍然处于新手模式:)看看这是否适合您(我忽略了您迄今为止尝试过的大部分代码,因为它似乎不必要地复杂,而且无论如何都无法运行):
results您应该尽量避免命名已经是基R函数的函数(即cor()
,您似乎在尝试使用这两种方法)。但是,我有点不明白你想做什么。。您只是想读取目录中的每个.csv并计算每个文件中两个字段之间的相关性吗?是的,这就是目标。额外的检查是检查每个.csv中的行/记录数是否大于阈值。这对我来说很好。通过这种方式学习文件操作:D。但是仍然不知道为什么我在read.csv中会出现错误
> cor("C:/SAT/Self Courses/R/data/specdata",0)
[1] 0
[1] "C:/SAT/Self Courses/R/data/specdata/001.csv"
Show Traceback
Rerun with Debug
Error in file(file, "rt") : cannot open the connection In addition: Warning message:
In file(file, "rt") :
cannot open file '7.21/001.csv': No such file or directory
results <- list()
threshold <- 0 # file must have this many lines to be processed
filepath <- "C:/SAT/Self Courses/R/data/specdata"
filenames <- list.files(filepath) # assumes you want all files in directory
suppressWarnings(
for(filename in filenames) {
# construct the path for this particular file, and read it
fpath <- paste(filepath, filename, sep="/")
input <- read.csv(fpath, header=TRUE)
# check if threshold is met, skip if not
if(nrow(input) <= threshold)) next
input <- na.omit(input) # do you want this before the threshold check?
# store our correlation in our results list
# stats::cor() to avoid confusion with your defined function
results[[filename]] <- stats::cor(input$sulfate, input$nitrate)
})
print(results)