I have .csv
files in a directory (lets say C:/Dowloads
). I am able to read all the files from that directory using the list.files("path")
. But I am unable to read a specified number of files using a for
loop. That is, lets say I have 332 files and I just want to read only files 1 to 10 or 5 to 10.
Here is an example:
files <- list.files("path")
files ## displays all the files.
Now for testing I did:
k <- files[1:10]
k
## here it displays the files from 1 to 10.
So I kept the same thing using a for
loop, as I want to read files one by one.
for(i in 1:length(k)){
length(i) ## just tested the length
}
But it is giving as NA
or Null
or 1
.
Can any one explain how can I read specified .csv
files using a for
loop or any other way?
list.files
return a character vector of class character
. A character vector is a vector of strings (i.e. characters). The function length
applied to a character vector files
or a range of elements within the character vector files[1:10]
or to a single element in a character vector files[i]
will return the number of strings in that character vector, the number of strings in the range, or 1, respectively. Use nchar
instead to get the number of characters for each element (each string) of the character vector. So:
path.to.csv <- "/path/to/your/csv/files"
files<-list.files(path.to.csv)
print(files) ## list all files in path
k<-files[1:10]
print(k) ## list first 10 files in path
for(i in 1:length(k)) { ## loop through the first 10 files
print(k[i]) ## each file name
print(nchar(k[i])) ## the number of characters in each file name
df <- read.csv(paste0(path.to.csv,"/",k[i])) ## read each as a csv file
## process each df in turn here
}
Note that we have to paste
the "path" to the file name in calling read.csv
.
EDIT: I thought I add this as an alternative:
path.to.csv <- "/path/to/your/csv/files"
files<-list.files(path.to.csv)
for(iFile in files) { ## loop through the files
print(iFile) ## each file name
print(nchar(iFile)) ## the number of characters in each file name
df <- read.csv(paste0(path.to.csv,"/",iFile)) ## read each as a csv file
## process each df in turn here
}
Here, the for
loop is over the collection (vector) of files
so that iFile
is the i
-th file name.
Hope this helps.
To read a specific number of files at a time you can subset your vector of files. First create a vector of your files, including a path:
f = list.files("/dir/dir", full.names=T, pattern="csv")
# nb full.names returns the full path to each file
Then, read each file to a separate list item (in this case, the first 10):
dl = lapply(f[1:10], read.csv)
Finally, have a look at list item 1:
head(dl[[1]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With