r - Opening csv of specific sequences: NAs come out of nowhere? -
i feel relatively straightforward question, , feel i'm close i'm not passing edge-case testing. have directory of csvs , instead of reading of them, want of them. files in format 001.csv, 002.csv,...,099.csv, 100.csv, 101.csv, etc should explain if()
logic in loop. example, files, i'd like:
id = 1:1000 setwd("d:/") filenames = as.character(null) (i in id){ if(i < 10){ <- paste("00",i,sep="") } else if(i < 100){ <- paste("0",i,sep="") } filenames[[i]] <- paste(i,".csv", sep="") } y <- do.call("rbind", lapply(filenames, read.csv, header = true))
the above code works fine id=1:1000
, id=1:10
, id=20:70
pass id=99:100
or sequence involving numbers starting @ on 100, introduces lot of nas.
example output below id=98:99
> filenames 098 099 "098.csv" "099.csv"
example output below id=99:100
> filenames 099 "099.csv" na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na na "100.csv"
i feel i'm missing catch statement in if()
logic. insight appreciated! :)
you can avoid loop creating filenames
filenames <- sprintf('%03d.csv', 1:1000) y <- do.call(rbind, lapply(filenames, read.csv, header = true))
Comments
Post a Comment