【问题标题】:Loop over a large number of CSV files with the same statements in R?在 R 中循环使用相同语句的大量 CSV 文件?
【发布时间】:2021-06-08 13:25:14
【问题描述】:

[已删除][已删除][已删除][已删除]

【问题讨论】:

    标签: r list loops csv


    【解决方案1】:

    这是读取所有文件并对其进行处理的一种方法。未经测试的代码,因为您没有给我们任何工作。

    # Get a list of CSV files. Use the path argument to point to a folder
    # other than the current working directory
    files <- list.files(pattern=".+\\.csv")
    
    # For each file, work your magic
    # lapply runs the function defined in the second argument on each
    # value of the first argument
    everything <- lapply(
      files,
      function(f) {
        values <- read.csv(f, header=FALSE)
        apply(values, 1, function(x) length(which(x==3)))
      }
    )
    # And returns the results in a list.  Each element consists of 
    # the results from one function call.
    # Make sure you can access the elements of the list by filename
    names(everything) <- files
    
    # The return value is a list.  Access all of it with
    everything
    
    # Or a single element with
    everything[["values04.csv"]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-30
      • 2019-04-04
      • 2017-06-02
      • 2016-03-05
      • 2022-07-05
      • 2011-04-25
      • 2022-06-21
      • 2021-11-19
      相关资源
      最近更新 更多