简体   繁体   English

如何使用R将合并文档中的列标题替换为文件目录?

[英]How to replace the title of columns in a merged document with the file directory using R?

I have performed an experiment under different conditions.我在不同条件下进行了实验。 Each of those condition has its own Folder.这些条件中的每一个都有自己的文件夹。 In each of those folders, there is a subfolder for each replicate that containts a text file called DistList.txt.在每个文件夹中,每个副本都有一个子文件夹,其中包含一个名为 DistList.txt 的文本文件。 This then looks like this, where the folders "C1.1", "C1.2" and so on contain the mentioned .txt files:然后看起来像这样,其中文件夹“C1.1”、“C1.2”等包含提到的 .txt 文件:

在此处输入图片说明

I have now managed to combine all those single DistList.txt files using the following script:我现在已经设法使用以下脚本组合所有这些单个 DistList.txt 文件:

setwd("~/Desktop/Experiment/.")

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")

This then yields a .csv file that has just numbers a titles (marked in red), which are not that useful for me, as shown in this picture:这将产生一个 .csv 文件,其中只有数字和标题(标记为红色),这对我来说不是那么有用,如下图所示:

在此处输入图片说明

I would rather like to have the directory of the "DistList.txt" files or even better only the name of the folder they are in as a title.我更希望拥有“DistList.txt”文件的目录,或者更好的是将它们所在的文件夹的名称作为标题。 I thought that I could do that using the function list.dirs() and colnames , but I somehow didn't manage to get it to work.我认为我可以使用函数list.dirs()colnames来做到这colnames ,但不知何故我没有设法让它工作。

I would be very grateful, if someone could help me with this issue!如果有人能帮助我解决这个问题,我将不胜感激!

I think this 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 .因为通过使用basename我们正在删除所有文件夹,留下文件名"DistList.txt" ,并且使用gsub将该文件名替换为空字符串""

We might actually want below instead, extract the last directory , which should give in your case something like c("C1.1", "C1.2", ...) :我们实际上可能想要在下面提取最后一个目录,在您的情况下应该给出类似c("C1.1", "C1.2", ...)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM