I am trying to pass directory as input to a function and use it as input to read.csv to read CSV files. However when during the process the read.csv is modifying the file name string sent at runtime.
Directory:"C:/SAT/Self Courses/R/data/specdata" Inside this directory there are number of CSV files i want to read and act upon with the following functions
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)
}
I execute this code with a call
cor("C:/SAT/Self Courses/R/data/specdata",0)
The error output which i get is
> 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
The problem is dir_temp : I have "C:/SAT/Self Courses/R/data/specdata/001.csv" however in the nextline read.csv is taking input '7.21/001.csv'
Please bear with me if the question seems trivial, i am still in Novice mode :)
cor()
, which you seem to be trying to use both ways here). But, I'm a little confused about what you are trying to do.. are you just trying to read every .csv in a directory and calculate the correlation between two fields that are in every file? – devmacrile