开发者

Writing a script for reading many .csv files with similar filenames

开发者 https://www.devze.com 2022-12-26 04:21 出处:网络
I have several .csv 开发者_开发百科files with similar filenames except a numeric month (i.e. 03_data.csv, 04_data.csv, 05_data.csv, etc.) that I\'d like to read into R.

I have several .csv 开发者_开发百科files with similar filenames except a numeric month (i.e. 03_data.csv, 04_data.csv, 05_data.csv, etc.) that I'd like to read into R.

I have two questions:

  • Is there a function in R similar to MATLAB's varname and assignin that will let me create/declare a variable name within a function or loop that will allow me to read the respective .csv file - i.e. 03_data.csv into 03_data data.frame, etc.? I want to write a quick loop to do this because the filenames are similar.
  • As an alternative, is it better to create one dataframe with the first file and then append the rest using a for loop? How would I do that?


You could look at this related question. You can create the file names easily with a paste command:

file.names <- paste(sprintf("%02d",1:10), "_data.csv", sep="")

Once you have your file names (whether by creating them or by reading them from the directory as in the other question), you can import them quickly with an lapply:

import.list <- lapply(file.names, read.csv)

Lastly, to combine the list into one dataframe, the easiest approach is to use the reshape function below:

library(reshape)
data <- merge_recurse(import.list)


It is also very easy to read the content of a directory including use of regular expressions to skip focus on certain names only, e.g.

filestoread <- list.files(someDir, pattern="\\.csv$", full.names=TRUE)

returns all (fully-formed, including full path) files in the given directory someDir that end on ".csv". You can get fancier with better regular expressions which are documented in many places.

Once you have your list of files, it is straightforward to read them all using apply or lapply or a loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号