Hi,
I have multiple .txt files (each file contains 2 columns; an identifier Gene
column and a sample column). I would like to merge the dataframes using the Gene
column. I was able to import multiple .txt files together, after importing, I am not been able to merge them using the loop in R. Please assist me regarding the same.
I usually merge the dataframe by using the
library(tidyverse)
Merge_All_Samples <- list(df1, df2, df3) %>% reduce(inner_join, by = "Gene")
Trying the below code to merge the dataframe/list, it does not work.
library(tidyverse)
all_files <- dir("/Users/Documents/Test/")
file_names <- grep(all_files,pattern = "^G.*.txt$",value = TRUE)
for(i in 1:length(file_names)){
Data_file <- read.delim(file = paste0("./",file_names[i]), stringsAsFactors = FALSE, check.names = FALSE, row.names = NULL)
##Merge all samples data
Merge_All_Samples <- list(Data_file) %>% reduce(inner_join, by = "Gene")
colnames(Merge_All_Samples)
##Export the merged data###
write.csv(Merge_All_Samples, "./Merge_All_Samples.csv", row.names = F)
}
Created on 2021-09-17 by the reprex package (v2.0.1)
dput(GSM1)
structure(list(Gene = c("A1BG", "A1BG-AS1", "A1CF", "A2M", "A2M-AS1"
), GSM1 = c(4L, 52L, 12L, 645L, 113L)), class = "data.frame", row.names = c(NA,
-5L))
#> Gene GSM1
#> 1 A1BG 4
#> 2 A1BG-AS1 52
#> 3 A1CF 12
#> 4 A2M 645
#> 5 A2M-AS1 113
dput(GSM2)
structure(list(Gene = c("A1BG", "A1BG-AS1", "A1CF", "A2M", "A2M-AS1"
), GSM2 = c(4L, 57L, 10L, 638L, 161L)), class = "data.frame", row.names = c(NA,
-5L))
#> Gene GSM2
#> 1 A1BG 4
#> 2 A1BG-AS1 57
#> 3 A1CF 10
#> 4 A2M 638
#> 5 A2M-AS1 161
dput(GSM3)
structure(list(Gene = c("A1BG", "A1BG-AS1", "A1CF", "A2M", "A2M-AS1"
), GSM3 = c(4L, 57L, 10L, 638L, 161L)), class = "data.frame", row.names = c(NA,
-5L))
#> Gene GSM3
#> 1 A1BG 4
#> 2 A1BG-AS1 57
#> 3 A1CF 10
#> 4 A2M 638
#> 5 A2M-AS1 161
Created on 2021-09-17 by the reprex package (v2.0.1)
Thank you,
Toufiq