Search code examples
rfilenamesdirectorynaming

Naming the columns of a merged file equal to the folder name the source file comes from


I have written a script in R that combines my text files having one column of data to a .csv file where all the columns are listed besides each other. Unfortunately, my analysis Software always lables the text file in the same way so that all the text files are called "List".

Hereby, I was able to combine the different text files to a .csv file.

fileList <- list.files(path = ".", recursive = TRUE, pattern = "DistList.txt", full.names = TRUE)

listData <- lapply(fileList, read.table)

names(listData) <- gsub("DistList.txt","",basename(fileList))

library(tidyverse)
library(reshape2)

bind_rows(listData, .id = "FileName") %>%
  group_by(FileName) %>%
  mutate(rowNum = row_number()) %>%
  dcast(rowNum~FileName, value.var = "V1") %>%
  select(-rowNum) %>%

write.csv(file="Result.csv")

Now, I would like to change the column names in such a way that it is equal to the name of the folder, in which the text file is located. As I don't have that much experience using R yet, I can't figure out, how I should do it.

Thank you very much for your help already in advance!


Solution

  • The line

    names(listData) <- gsub("DistList.txt", "", basename(fileList))
    

    should be:

    names(listData) <- gsub("DistList.txt", "", fileList)
    

    Because by using basename we are removing all the folders, leaving us with filename "DistList.txt", and that filename gets replaced by empty string "" using gsub.

    We might actually want below instead, extract the last directory, which should give in this case something like c("C1.1", "C1.2", ...):

    names(listData) <- basename(dirname(fileList))