Рет қаралды 9,568
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