0

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

Hugh
  • 15,521
  • 12
  • 57
  • 100
Tarku
  • 1
  • 1
  • 1
    Indent your code lines by 4 spaces to get it to format properly on SO. All code should be minimal, self contained and complete including all library statements, if any, and all inputs. These 3 links contain more on how to ask questions: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example http://stackoverflow.com/help/mcve http://stackoverflow.com/help/how-to-ask – G. Grothendieck Nov 01 '15 at 02:13

2 Answers2

1

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))
}
Constantine
  • 479
  • 1
  • 9
  • 19
0

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)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thnak you very much, this got me further in the process, but now I have another error: Error in matrix(ncol = 3, nrow = as.integer(tmpVoidPoints), data = 0) : invalid 'nrow' value (too large or NA) This is weird because I am stablishin my rows as: nrow = as.integer(tmpVoidPoints) – Tarku Nov 01 '15 at 18:51
  • The expected response to spotting an error and offering effective advice is to upvote or check the answer. If you then have a new problem that is unrelated to the first problem then it's time for a new question which would need to have a proper description of whatever file is being worked on. – IRTFM Nov 01 '15 at 19:39