Import & Merge Multiple CSV Files in R (Example) | dplyr, plyr & readr Packages | How to Load & Read

  Рет қаралды 9,568

Statistics Globe

Statistics Globe

Күн бұрын

How to import and merge CSV files in the R programming language. More details: statisticsglob...
R code of this video:
data1 <- data.frame(id = 1:6, # Create first example data frame
x1 = c(5, 1, 4, 9, 1, 2),
x2 = c("A", "Y", "G", "F", "G", "Y"))
data2 <- data.frame(id = 4:9, # Create second example data frame
y1 = c(3, 3, 4, 1, 2, 9),
y2 = c("a", "x", "a", "x", "a", "x"))
data3 <- data.frame(id = 5:6, # Create third example data frame
z1 = c(3, 2),
z2 = c("K", "b"))
write.csv(data1, "C:/Users/Joach/Desktop/my_folder/data1.csv", # Write first example data frame
row.names = FALSE)
write.csv(data2, "C:/Users/Joach/Desktop/my_folder/data2.csv", # Write second example data frame
row.names = FALSE)
write.csv(data3, "C:/Users/Joach/Desktop/my_folder/data3.csv", # Write third example data frame
row.names = FALSE)
install.packages("dplyr") # Install dplyr package
install.packages("plyr") # Install plyr package
install.packages("readr") # Install readr package
library("dplyr") # Load dplyr package
library("plyr") # Load plyr package
library("readr") # Load readr package
data_all <- list.files(path = "C:/Users/Joach/Desktop/my_folder", # Identify all csv files in folder
pattern = "*.csv", full.names = TRUE) %>%
lapply(read_csv) %>% # Store all files in list
bind_rows # Combine data sets into one data set
data_all_df <- as.data.frame(data_all) # Convert tibble to data.frame
install.packages("purrr") # Install & load purrr package
library("purrr")
data_join <- list.files(path = "C:/Users/Joach/Desktop/my_folder", # Identify all csv files in folder
pattern = "*.csv", full.names = TRUE) %>%
lapply(read_csv) %>% # Store all files in list
purrr::reduce(full_join, by = "id") # Full-join data sets into one data set
Follow me on Social Media:
Facebook - Statistics Globe Page: / statisticsglobecom
Facebook - R Programming Group for Discussions & Questions: / statisticsglobe
Facebook - Python Programming Group for Discussions & Questions: / statisticsglobepython
LinkedIn - Statistics Globe Page: / statisticsglobe
LinkedIn - R Programming Group for Discussions & Questions: / 12555223
LinkedIn - Python Programming Group for Discussions & Questions: / 12673534
Twitter: / joachimschork
Music by bensound.com

Пікірлер: 12
@surajbhagat2672
@surajbhagat2672 10 ай бұрын
how to add new column in combined data describing the each DataName where do those data belong?
@cansustatisticsglobe
@cansustatisticsglobe 10 ай бұрын
Hello! Please see the following R code. Where I defined a special function that reads the CSV elements and retrieves the source name using the gsub function (statisticsglobe.com/sub-gsub-r-function-example) and then creates a source_name column using the mutate function (statisticsglobe.com/r-mutate-transmute-functions-dplyr-package). library("dplyr") library("plyr") library("readr") # Function to read each CSV file and add a source column read_csv_with_source
@amane49
@amane49 9 ай бұрын
Another amazing to the point video !! Easy to follow, logical and very helpful!
@matthias.statisticsglobe
@matthias.statisticsglobe 9 ай бұрын
What a great and motivating comment! Thank you very much for your support!
@vishakhakarnawat
@vishakhakarnawat Жыл бұрын
Thanks. Could you also show how to merge multiple .txt files excluding headers and get the resultant output in .txt?
@cansustatisticsglobe
@cansustatisticsglobe Жыл бұрын
Hello Vishakha, Would you like to implement something like this? data1
@danielmoreno1097
@danielmoreno1097 Жыл бұрын
Thx, this help me so much, greetings from Colombia
@matthias.statisticsglobe
@matthias.statisticsglobe Жыл бұрын
That's great to hear, thank you so much for the positive feedback, Daniel! Greetings back from Germany
@krono32
@krono32 3 ай бұрын
This helped me with a project. Thank you!
@micha.statisticsglobe
@micha.statisticsglobe 3 ай бұрын
Thanks a lot for your feedback. Glad it helped! 🙂
@Sara-LeeRamsawak-s6x
@Sara-LeeRamsawak-s6x 8 ай бұрын
Thanks so much, this is very helpful. What does this row of code do? --> data_all_df <- as.data.frame(data_all) # Convert tibble to data.frame
@Ifeanyi.StatisticsGlobe
@Ifeanyi.StatisticsGlobe 8 ай бұрын
Hi User-MV. Yes, as.data.frame( ) converts a tibble into a dataframe. You can confirm this by running str( ) on the new converted dataframe object. I hope this helps!
💩Поу и Поулина ☠️МОЧАТ 😖Хмурых Тварей?!
00:34
Ной Анимация
Рет қаралды 2 МЛН
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45
路飞与唐舞桐
Рет қаралды 5 МЛН
Inside Out 2: ENVY & DISGUST STOLE JOY's DRINKS!!
00:32
AnythingAlexia
Рет қаралды 13 МЛН
HAH Chaos in the Bathroom 🚽✨ Smart Tools for the Throne 😜
00:49
123 GO! Kevin
Рет қаралды 16 МЛН
Importing a .csv file to R Studio using the read.csv function
5:56
Charlene McCord
Рет қаралды 266 М.
Нашумевшая задача математического клуба
6:01
Математика и фокусы
Рет қаралды 6 М.
How to speak like the 1% elite
10:30
Joseph Tsar
Рет қаралды 14 М.
💩Поу и Поулина ☠️МОЧАТ 😖Хмурых Тварей?!
00:34
Ной Анимация
Рет қаралды 2 МЛН