简体   繁体   中英

Cannot Read csv file

I am new to R and I have this proplem: I have a set of csv files, each have 3 colums with numeric values. I am traying to run a set of instructions to create categories that allow me to align the graphs of each of the files. This is so far my set of commands:

myFiles<-dir("C:\\Data\\")
myDeadVols<-as.matrix(read.csv("C:\\Data\\Dead Volumes.csv", header=T, sep="|", row.names=1))

myDeadVolsVec <- as.numeric(myDeadVols[,"t2_min"])*60 + as.numeric(myDeadVols[,"t2_s"])

names(myDeadVolsVec) <- myDeadVols[,"fileName"]

sampAnot <- names(myDeadVolsVec)

names(sampAnot) <- sampAnot

polyRead <- function(fileNames=NULL, mySep="|"){

dataList <- list()

for(tmpName in fileNames){dataList[[tmpName]] <-as.matrix(read.table(tmpName, header=TRUE, sep=mySep))}
return(dataList)}

polyReadMaritza <- function(fileNames=NULL, mySep="|", file2void=NA, fracTime=35, dataPerFrac=173){

dataList <- list()
for(tmpName in fileNames){
tmpMat<-as.matrix(read.table(tmpName, header=FALSE, sep=mySep))
tmpVoidTime<-file2void[tmpName]
tmpVoidPoints<-tmpVoidTime/fracTime*dataPerFrac
tmpAdder<-matrix(ncol=3, nrow=as.integer(tmpVoidPoints), data=0)
tmpMat<-rbind(tmpAdder, tmpMat)
tmpEvent<-rep(0, dim(tmpMat)[1])
fractMoves<-c(1:length(tmpEvent))[which(c(1:length(tmpEvent))%%dataPerFrac==0)]
tmpEvent[fractMoves]<-1
tmpMat[,3]<-tmpEvent
dataList[[tmpName]] <- tmpMat
colnames(dataList[[tmpName]])<-c("Distance.mm.", "Absorbance", "Event")}
return(dataList)}

So far everything seems ok but when I try to enter the fllowin command:

myDataList <- polyReadMaritza(myFiles, mySep="\t", file2void=myDeadVolsVec)

I have this error:

Error in file(file, "rt") : cannot open the connection. In addition: Warning message:In file(file, "rt") : cannot open file 'Beads.csv': No such file or directory

Now, the path to 'Beads.csv' is C:\\Data\\Beads.csv, so I think that is because It does not read the complet path to 'Beads.csv' (which is the first of the files) but I thought that with "myFiles<-dir("C:\\Data\\")" I already specified the path. Any help is welcomed

You have actually get the list of files in the desired directory.

fileDir = 'C:/Data'
myFiles = list.files(path=fileDir, pattern='*.csv')

Then when you call read.csv you have to give R the full filepath with...

for(file in myFiles){
    read.csv(file.path(fileDir,file))
}

The value that was returned by dir did not include paths. The default for "full.names" in dir is FALSE so unless your working directory is 'c://Data//' the read.csv call will not be looking in the correct location. Try:

myFiles <- dir("C:\\Data\\", pattern= "[.]csv", full.names-TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM