How can I import multiple csv files into R

0 votes
Apr 20, 2018 in Data Analytics by shams
• 3,670 points
6,263 views

1 answer to this question.

0 votes
## import_multiple_csv_files_to_R
# Purpose: Import multiple csv files to the Global Environment in R

# set working directory
setwd("~/R/projects/tutorials/import_multiple_data_to_R/")

# list all csv files from the current directory
list.files(pattern=".csv$") # use the pattern argument to define a common pattern  for import files with regex. Here: .csv

# create a list from these files
list.filenames<-list.files(pattern=".csv$")
list.filenames

# create an empty list that will serve as a container to receive the incoming files
list.data<-list()

# create a loop to read in your data
for (i in 1:length(list.filenames))
{
list.data[[i]]<-read.csv(list.filenames[i])
}

# add the names of your data to the list
names(list.data)<-list.filenames

# now you can index one of your tables like this
list.data$deforestation2010.csv

# or this
list.data[1]

# you can make a function out of this
import.multiple.csv.files<-function(mypath,mypattern,...)
{
tmp.list.1<-list.files(mypath, pattern=mypattern)
tmp.list.2<-list(length=length(tmp.list.1))
for (i in 1:length(tmp.list.1)){tmp.list.2[[i]]<-read.csv(tmp.list.1[i],...)}
names(tmp.list.2)<-tmp.list.1
tmp.list.2
}

# use it like this
csv.import<-import.multiple.csv.files("~/R/projects/tutorials/import_multiple_data_to_R/",".csv$",sep=",")
# note: with ... we enable the function to refine the import with parameters from read.csv.
# here we define the separator of entries in the csv files to be comma.

# save it to the folder with your custom functions
save(import.multiple.csv.files,file="~/R/functions/import.multiple.csv.files.RData")

# load it like this whenever you need it in another script with
load("~/R/functions/import.multiple.csv.files.RData")

# end
answered Apr 20, 2018 by zombie
• 3,790 points

Related Questions In Data Analytics

0 votes
1 answer

How can I delete multiple values from a vector in R?

The %in% operator tells  which elements are ...READ MORE

answered Apr 27, 2018 in Data Analytics by shams
• 3,670 points
6,209 views
0 votes
1 answer

How can I read a .csv file in R language?

read.csv () function is used to read ...READ MORE

answered Jun 12, 2018 in Data Analytics by zombie
• 3,790 points
763 views
0 votes
1 answer

How can I import a file in R without giving the destination/file path to the specified file?

You can use the window explorer to ...READ MORE

answered Aug 28, 2018 in Data Analytics by Abhi
• 3,720 points

edited Aug 28, 2018 by Vardhan 3,038 views
0 votes
1 answer

How do I import document files in R programming

Try the following: read.table("~/SMSSpamCollection", header=TRUE, sep="\t") Change "~/" to the "ptah/to/your/file/" Mess around ...READ MORE

answered Nov 5, 2018 in Data Analytics by Kalgi
• 52,350 points
948 views
+7 votes
4 answers

How can I include javascript code into my shiny R dashboard?

There are multiple ways doing this. One ...READ MORE

answered Dec 5, 2018 in Data Analytics by Maverick
• 10,840 points
6,602 views
0 votes
0 answers

How can I avoid the transformation into an atomic vector in a matrix (r programming)

I'm scrambling to come up with a ...READ MORE

Jul 5, 2022 in Data Analytics by avinash
• 1,840 points
469 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,963 views
0 votes
1 answer

How can I measuring running time of R Code ?

5 ways to measure running time of ...READ MORE

answered Apr 17, 2018 in Data Analytics by anonymous
12,790 views
0 votes
1 answer

How can I use R to list all files with a specified extension ?

fls <- list.files(pattern = "\\.dbf$") $ at the ...READ MORE

answered May 11, 2018 in Data Analytics by zombie
• 3,790 points
903 views
0 votes
1 answer

How can I Split code over multiple lines in an R script?

You can do this as follows: setwd(paste("~/a/very/long/path/here ...READ MORE

answered May 22, 2018 in Data Analytics by zombie
• 3,790 points
2,002 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP