I have 100 files.txt in a folder.
Those filenames.txt are written identically into column1 rows of a R dataframe.
The files.txt are less than the dataframe column1 rows! Meaning that not all the rows of column1 will be matched!
What I would like to do :
If the filename match column1 name, then, insert names of column 2 and 3 on the same row (R dataframe), this as columns in the file.txt.
Example
Name Family Subfamily
marc A B
Jaco C D
marc.txt
Jaco.txt
Out-put files.txt containing the new columns family and subfamily.
marc.txt
column1 column2 ..... Family Subfamily
..... ...... ..... A B
..... ..... ..... A B
..... ..... ...... A B
jaco.txt
column1 column2 ..... Family Subfamily
..... ...... ..... C D
..... ..... ..... C D
..... ..... .... C D
Edit: created check to see if file exists
Something like this should work:
df1 <- data.frame(Name = c("marc", "Jaco"),
Family = c("A.", "C"),
Subfamily = c("B", "D"))
files <- list.files(pattern = ".txt")
for (i in 1:nrow(df1)) {
if (!paste0(df1[i,1], ".txt") %in% files) {
next()
}
dfi <- read.csv(paste0(df1[i,1], ".txt"))
dfi <- cbind(dfi, df1[i,-1])
write.csv(dfi, paste0(df1[i,1], ".txt"))
}
Just need to replace the table names