gpt4 book ai didi

r - 在 R 中操作多个文件

转载 作者:行者123 更新时间:2023-12-01 11:56:44 25 4
gpt4 key购买 nike

我是 R 新手,正在寻找一种代码来操作我手头的数百个文件。它们是带有几行不需要的文本的 .txt 文件,后面是数据列,如下所示:

XXXXX 
XXXXX
XXXXX
Col1 Col2 Col3 Col4 Col5
1 36 37 35 36
2 34 34 36 37
.
.
1500 34 35 36 35

我编写了一个代码(如下)来提取单个 .txt 文件的第 1 列和第 5 列的选定行,并希望对我拥有的所有文件进行循环。
data <- read.table(paste("/Users/tan/Desktop/test/01.txt"), skip =264, nrows = 932)
selcol<-c("V1", "V5")
write.table(data[selcol], file="/Users/tan/Desktop/test/01ed.txt", sep="\t")

使用上面的代码,.txt 文件现在看起来像这样:
Col1 Col5  
300 34
.
.
700 34

如果可能的话,我想将 .txt 文件的所有 Col5 与第 1 列之一(所有 txt 文件都相同)结合起来,使其看起来像这样:
Col1 Col5a Col5b Col5c Col5d ...
300 34 34 36 37
.
.
700 34 34 36 37

谢谢!

最佳答案

好吧 - 我想我在这里找到了你所有的问题,但如果我错过了什么,请告诉我。我们将在这里经历的一般过程是:

  • 确定我们要在工作目录中读取和处理的所有文件
  • 使用 lapply 遍历每个文件名以创建包含所有数据的单个列表对象
  • 选择您感兴趣的列
  • 通过公共(public)列将它们合并在一起

  • 出于示例的目的,考虑我有四个名为 file1.txtfile4.txt 的文件,它们看起来都像这样:
        x           y          y2
    1 1 2.44281173 -2.32777987
    2 2 -0.32999022 -0.60991623
    3 3 0.74954561 0.03761497
    4 4 -0.44374491 -1.65062852
    5 5 0.79140012 0.40717932
    6 6 -0.38517329 -0.64859906
    7 7 0.92959219 -1.27056731
    8 8 0.47004041 2.52418636
    9 9 -0.73437337 0.47071120
    10 10 0.48385902 1.37193941

    ##1. identify files to read in
    filesToProcess <- dir(pattern = "file.*\\.txt$")
    > filesToProcess
    [1] "file1.txt" "file2.txt" "file3.txt" "file4.txt"


    ##2. Iterate over each of those file names with lapply
    listOfFiles <- lapply(filesToProcess, function(x) read.table(x, header = TRUE))

    ##3. Select columns x and y2 from each of the objects in our list
    listOfFiles <- lapply(listOfFiles, function(z) z[c("x", "y2")])

    ##NOTE: you can combine steps 2 and 3 by passing in the colClasses parameter to read.table.
    #That code would be:
    listOfFiles <- lapply(filesToProcess, function(x) read.table(x, header = TRUE
    , colClasses = c("integer","NULL","numeric")))

    ##4. Merge all of the objects in the list together with Reduce.
    # x is the common columns to join on
    out <- Reduce(function(x,y) {merge(x,y, by = "x")}, listOfFiles)
    #clean up the column names
    colnames(out) <- c("x", sub("\\.txt", "", filesToProcess))

    结果如下:
    > out
    x file1 file2 file3 file4
    1 1 -2.32777987 -0.671934857 -2.32777987 -0.671934857
    2 2 -0.60991623 -0.822505224 -0.60991623 -0.822505224
    3 3 0.03761497 0.049694686 0.03761497 0.049694686
    4 4 -1.65062852 -1.173863215 -1.65062852 -1.173863215
    5 5 0.40717932 1.189763270 0.40717932 1.189763270
    6 6 -0.64859906 0.610462808 -0.64859906 0.610462808
    7 7 -1.27056731 0.928107752 -1.27056731 0.928107752
    8 8 2.52418636 -0.856625895 2.52418636 -0.856625895
    9 9 0.47071120 -1.290480033 0.47071120 -1.290480033
    10 10 1.37193941 -0.235659079 1.37193941 -0.235659079

    关于r - 在 R 中操作多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6420207/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com