I'm trying to write a script that automatically concatenates all files from specific folders automatically to one .txt file but I'm having problems because I'm trying to merge them into one big data frame before writing it into a txt file and I'm getting errors because column names didn't match, so I used smartbind, but not I'm getting an error for "double row names".
Here's my code:
library(gtools)
dir<-"/Users/max/Desktop/NISAT_All/Regions"
subdir_list<-list.dirs(dir, recursive=F)
subdir_list<-list.dirs(subdir_list, recursive=F)
as.matrix(subdir_list)
subdirs_General <- subdir_list[ grepl("General", subdir_list) ]
as.matrix(subdirs_General)
subdir_list <- subdir_list[ !grepl("General", subdir_list) ]
subdir_list<-list.dirs(subdir_list, recursive=F)
as.matrix(subdir_list)
for (subdir in subdir_list){
setwd(subdir)
subdir <-list.files(subdir, recursive=T)
files <- subdir[ grepl("Armed Groups and Small Guns", subdir) ]
files <- c(files, subdir[ grepl("Arms Embargoes", subdir) ])
files <- c(files, subdir[ grepl("Black Market", subdir) ])
files <- c(files, subdir[ grepl("Brokering", subdir) ])
files <- c(files, subdir[ grepl("Landmines", subdir) ])
files <- c(files, subdir[ grepl("MANPADS", subdir) ])
files <- c(files, subdir[ grepl("Production", subdir) ])
files <- c(files, subdir[ grepl("Stockpile Security and Destruction", subdir) ])
files <- c(files, subdir[ grepl("UN Processes", subdir) ])
files <- c(files, subdir[ grepl("United Nations", subdir) ])
files <- c(files, subdir[ grepl("Weapons Collection and Amnesties", subdir) ])
dataframe <- data.frame()
for (file in files){
df_temp <- read.delim(file)
dataframe <- smartbind(dataframe, df_temp, sep="\n")
}
#then write your final file
write.table(dataframe,"MergedFiles.txt",sep="\n", row.names = F, eol = "\r")
rm(dataframe)
}
on the picture you can see how the files usually look like, I just wanna merge them into one
Can anyone help?