【问题标题】:Combine the multiple files with one header将多个文件与一个标题合并
【发布时间】:2010-11-20 18:06:07
【问题描述】:

我有五个由R 程序生成的文本文件(每个文件都有标题),它们要组合成一个文件。我使用rbind 将它们组合在一起,我的问题是当我组合它们时,结果输出在每个文件的末尾附加了标题,例如, 如果标题是应该的

Combine Resultant file
A  B  C  D
1  3  5  7    ------------> Text file1
6  9  0  3
A  B  C  D
1  3  6  7    ------------> Text file 2
5  7  8  3
and so on....

我希望输出文件在第 1 行只有一个标题 所以文件应该是这样的:

Combine Resultant file
A  B  C  D
1  3  5  7    ------------> Text file1
6  9  0  3
1  3  6  7    ------------> Text file 2
5  7  8  3
and so on....

谁能告诉我该怎么做? 我的代码是:

S1 <- read.table("C:/Simulation_Results/sim_1_200.txt",sep=";",header= TRUE);
S2 <- read.table("C:/Simulation_Results/sim_201_400.txt", sep=";",header= TRUE);
S3 <- read.table("C:/Simulation_Results/sim_401_600_b.txt", sep=";",header= TRUE);
S4 <- read.table("C:/Simulation_Results/sim_601_800.txt", sep=";",header= TRUE);
S5 <- read.table("C:/Simulation_Results/sim_901_1000.txt",sep=";",header= TRUE);
S6 <- read.table("C:/Simulation_Results/simulations_801_900.txt",sep=";",header= TRUE);
options(max.print=28.5E6);
S7 <- rbind(S1,S2,S3,S4,S5,S6)
write.table(S7, file="C:/Simulation_Results/simulation_1_1000.txt", sep=";", 
            row.names=TRUE, col.names=FALSE, quote = FALSE);

谢谢!

【问题讨论】:

  • 这对我来说是正确的。可能错误出在其他程序生成的 txt 文件中。我们可以看看head(S1)tail(S1),好吗?此外,对问题中的代码进行格式化也很有帮助(突出显示代码部分并按下代码按钮——上面有 1010 的那个)。
  • 我们能看到S1的头部和尾部吗?问题的一部分是逗号和小数吗?就像 Dwin 指出的那样,您编写 txt 文件的 R 代码可能是错误的标题。
  • 你有生成文件的代码吗?如果是这样,我只需将表格保存到单个变量 x1

标签: r


【解决方案1】:

这听起来像是不正确的导入。您应该提供示例数据。不管怎样,不要使用read.table("some.file", header=TRUE, sep=";"),而是试试read.csv2,因为它有header=TRUEsep=";" 作为默认参数。

而不是rbind,你为什么不使用merge

【讨论】:

    【解决方案2】:

    是的;您的输入数据与您的读取函数不匹配存在问题,并且您正在使用错误的 sep 参数读取数据。我认为您需要跳过一行才能到达标题所在的位置。试试这个:

    S1 <- read.table("C:/Simulation_Results/sim_1_200.txt", header= TRUE, skip=1)
    S2 <- read.table("C:/Simulation_Results/sim_201_400.txt",  skip=1, header= TRUE)
    S3 <- read.table("C:/Simulation_Results/sim_401_600_b.txt", skip=1 , header= TRUE)
    S4 <- read.table("C:/Simulation_Results/sim_601_800.txt", skip=1, header= TRUE)
    S5 <- read.table("C:/Simulation_Results/sim_901_1000.txt", skip=1 , header= TRUE)
    S6 <- read.table("C:/Simulation_Results/simulations_801_900.txt", skip=1, header= TRUE)
    

    然后像以前一样继续。 sep=";"正在阻止识别空格分隔,并且前导行中有某些内容导致您的标题未转换为列名。

    【讨论】:

      【解决方案3】:

      跟进 DWin:怎么样

      startvals <- c(1,201,401,601,801,901)
      endvals   <- c(startvals[-1]-1,1000)
      fns <- paste("C:/Simulation_Results/",startvals,"_",endvals,".txt",sep="")
      ## I'm assuming for the moment that your S6 file is *not* named differently from the others
      S <- lapply(fns, read.csv2, skip=1)
      S_comb <- do.call(rbind,S)
      

      如果您以特定方式命名您的模拟文件,以便您可以使用 list.files(pattern="[some regular expression]") 来识别它们,那么您可以这样开始......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多