Hello,
I have a list of lists. The lists in the list of lists are file names. I use lapply to read and merge the contents of each list in the list of lists (3 merged contents in this case which will be the content of 3 files). Then, I have to change the name of the 3 resulting files and finally I have to write the contents of the files to each file. In this case the files I am reading and merging are the repeated .txt file which only contains the data a b c d e f separated by tabs.
lc <- list("test.txt", "test.txt", "test.txt", "test.txt")
lc1 <- list("test.txt", "test.txt", "test.txt")
lc2 <- list("test.txt", "test.txt")
#list of lists. The lists contain file names
lc <- list(lc, lc1, lc2)
#new names for the three lists in the list of lists
new_dataFns <- list("name1", "name2", "name3")
file_paths <- NULL
new_path <- NULL
#add the file names to the path and read and merge the contents of each list in the list of lists
lapply(
lc,
function(lc) {
filenames <- file.path(dataFnsDir, lc)
dataList= lapply(filenames, function (x) read.table(file=x, header=TRUE))
Reduce(function(x,y) merge(x,y), dataList)
# print(dataList)
}
)
#add the new name of the file to the path total will be 3 paths/fille_newname.tsv.
lapply(new_path, function(new_path){new_path <- file.path(getwd(), new_dataFns)
The statements above work because lc and new_dataFns are global and I can pass them to the lapply function
#Finally, I need to write the merged contents to the corresponding file (p\ath/name.tsv). I tried the following statement, but this does not work. How can I write the content to each file? I was trying to use list <- cbind(dataList, new_path) so that afterwards I can get the merged contents and the file_name from the list and that way write each merged content to the corresponding file, but it seems that the dataList and the newPath are not global and the cbind() function does not work.