function - R: cannot open file 'specdata/001.csv': No such file or directory -


i'm pretty new r, , after researching error extensively, i'm still not able find solution. function created in r determine complete cases in directory 332 .csv files.

complete <- function(directory, id = 1:332) {   s <- vector()   (i in 1:length(id)) {     path <- c(paste(directory, "/",formatc(id[i], width=3, flag=0),".csv",sep=""))        data <- c(read.csv(path))      s[i] <- sum(complete.cases(data))   }    dat <- data.frame(cbind(id,nobs=s))      return(dat) } 

when want test function, giving following command (specdata directory .csv files stored)

complete("specdata", 1) 

i keep getting following error:

error in file(file, "rt") : cannot open connection in addition: warning message: in file(file, "rt") :   cannot open file 'specdata/001.csv': no such file or directory 
  • i checked working directory
  • i checked files within directory cannot detect problems there.

this happening because working directory not set location containing files. happened me well. figured out hard-coding location of directory in function.

complete<-function(directory,id=1:332) {   directory=file.path(getwd())   observations=0   counts = c()   for(i in id)   {     name=sprintf("%03d.csv", i)     data<-read.csv(name, sep="",header= t,na.strings=c("na","nan",""))     data=na.omit(data)     counts=append(counts, nrow(data))   }     df <- data.frame(id=id, nobs=counts)   df } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -