Read multiple csv files (and skip 2 columns in each csv file) into one dataframe in R?

dataguy132

I have a folder of about 100 csv files and I want to read them into one dataframe in R. I kind of know how to do this but I have to skip the first two columns in every csv file and that is the part I am stuck on. My code so far is:

myfiles <- list.files(pattern = ".csv") # create a list of all csv files in the directory
data_csv <- ldply(myfiles, read.csv)

Thank you for any help

Matt Summersgill

Using the data.table package functions fread() and rbindlist() will provide the result you're after faster than any of the other base or tidyverse alternatives.

library(data.table)

## Create a list of the files
FileList <- list.files(pattern = ".csv")

## Pre-allocate a list to store all of the results of reading
## so that we aren't re-copying the list for each iteration
DTList <- vector(mode = "list", length = length(FileList))

## Read in all the files, excluding the first two columns
for(i %in% seq_along(DTList)) {
  DTList[[i]] <- data.table::fread(FileList[[i]], drop = c(1,2))
}

## Combine the results into a single data.table
DT <- data.table::rbindlist(DTList)

## Optionally, convert the data.table to a data.frame to match requested result
## Though I would recommend looking into using data.table instead!
data.table::setDF(DT)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Upload multiple csv files and select same columns from each into one dataframe in R?

Merge several columns from multiple csv files to one csv file

Python numpy, skip columns & read csv file

Reading multiple csv files and getting the filename of each csv file in R

Read multiple csv files into a single dataframe and rename columns based on file of origin - Pandas

each two columns in the excel file are separated into multiple csv files

spread a file into multiple .csv files per each line of inputfile read

Read csv file where values of one variable are expanding to multiple line for each observation in R

Combining multiple csv files into one csv file

Powershell read in csv columns from large number of files then output to new csv where each file is a new column

Merge 2 CSV Files into one CSV File

Load csv files with multiple columns into several dataframe

Pyspark read multiple csv files into a dataframe (OR RDD?)

Pyspark read multiple csv files into a dataframe in order

How best to read multiple csvs into a single dataframe when each csv has multiple common columns

Merge Multiple CSV files to one with different columns

Read from one csv file and write to different multiple csv files depending on content

Loading multiple csv files of a folder into one dataframe

Load Multiple CSV files into one DataFrame with multilevel

Append csv files in multiple folders into one dataframe

Splitting a csv file into panda dataframe by multiple columns

Split CSV file Columns into multiple files with iloc

Read in multiple files using pd.read_csv() and save each file as a different variable

Insert and rename columns in multiple csv files and merging into one csv

Merging CSV files with a single column into one CSV file with 14 columns

Multiple pandas.dataframe to one csv file

Read into multiple .csv files

Read multiple csv data files and sort the data into a new csv file

Compare 2 columns in one csv file(percentages)