Reputation: 1
I've been trying to figure out a way to get the number of rows for a certain number of CSVs. I can get R to output the number of rows (minus the NAs) for one file.
But when I try to use the Colon Operator I keep getting an error:
Error in file(file, "rt") : invalid 'description' argument
The auction I created:
complete <- function(directory,id = 1:332){
## 'directory is a character vector of length 1 indicating
## the location of the CSV files
## 'id' is an intiger vector indicating the monitor ID numbers
## to be used
## Return a data frame of the form:
## id nobs
## 1 117
## 2 1041
## ...
## where 'id' is the monitor ID and 'nobs' is the
## number of complete cases
filenames <- sprintf("%03d.csv", id)
filenames <- paste(directory, filenames, sep="/")
file1 <- read.csv(filenames)
n_row <- NROW(na.omit(file1))
output <- data.frame(id,nobs=n_row)
output
}
I tried using a for loop to loop read each file:
for (i in id){
filenames <- sprintf("%03d.csv", id)
filenames <- paste(directory, filenames, sep="/")
file1 <- read.csv(filenames)
n_row <- NROW(na.omit(file1))
output <- data.frame(id,nobs=n_row)
}
output
That still comes up with the Error:
Error in file(file, "rt") : invalid 'description' argument
Any hints on where I can go from here?
Thanks
Upvotes: 0
Views: 60
Reputation: 93761
If you have a vector filenames
containing path to each file, then something like this should work:
fl = sapply(filenames, function(x) {
dat = read.csv(x)
return(data.frame(file=x, nobs=nrow(na.omit(dat))))
}, simplify=FALSE)
do.call(rbind, fl)
Upvotes: 1
Reputation: 41
if your files are named as 1.csv,2.csv,3.csv.
id=1:4
df=NULL
for (i in 1:length(id)) {
n_row<-NROW(na.omit(read.csv(paste0("C:/Users/fol/",id[i],".csv"))))
df=rbind(df,data.frame(id=id[i],nobs=n_row))
}
Upvotes: 0