Importing and binding multiple and specific csv fi

2019-03-01 01:32发布

问题:

I would like to import and bind, all together in a R file, specific csv files named as "number.CSV" (e.g. 3437.CSV) which I have got in a folder with other csv files that I do not want to import.

How can I select only the ones that interest me?

I have got a list of all the csv files that I need and in the following column there are some of them.

CODE
49002
47001
64002
84008
46003
45001
55008
79005
84014
84009
45003
45005
51001
55012
67005
19004
7003
55023
55003
76004
21013

I have got 364 csv files to read and bind.

n.b. I can't select all the "***.csv" files from my folder because I have got other files that I do not need.

Thanks

回答1:

Just make file names out of your numeric codes:

filenames = paste(code, 'csv', sep = '.')
# [1] "49002.csv" "47001.csv" "64002.csv" …

You might need to specify the full path to the files as well:

directory = '/example/path'
filenames = file.path(directory, filenames)
# [1] "/example/path/49002.csv" "/example/path/47001.csv" "/example/path/64002.csv" …

And now you can simply read them into R in one go:

data = lapply(filenames, read.csv)

Or, if your CSV files don’t have column headers (this is the case, in particular, when the file’s lines have different numbers of items!)

data = lapply(filenames, read.csv, header = FALSE)

This will give you a list of data.frames. If you want to bind them all into one table, use

data = do.call(rbind, data)


回答2:

You could iterate over the list of CSV files of interest, read in each one, and bind it to a common data frame:

path <- "path/to/folder/"
ROOT <- c("49002", "47001", "21013")
files <- paste0(path, ROOT)
sapply(files, bindFile, var2=all_files_df)

bindFile <- function(x, all_df) {
    df <- read.csv(x)
    all_df <- rbind(df, all_df)
}


回答3:

I don't know if you can do that from .CSV file. What you can do is open all your data and then use the command cbind.

For example:

data1 <- read.table("~/YOUR/DATA", quote="\"", comment.char="")
data2 <- read.table("~/YOUR/DATA", quote="\"", comment.char="")
data3 <- read.table("~/YOUR/DATA", quote="\"", comment.char="")

And then:

df <- cbind(data1$Col1, data2$col3...)

Where col is the name of the column that you want.



标签: r csv import bind