简体   繁体   English

循环使用 R 中相同语句的大量 CSV 文件?

[英]Loop over a large number of CSV files with the same statements in R?

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

Here's one way you can read all the files and proceess them.这是您可以读取所有文件并对其进行处理的一种方法。 Untested code as you haven't given us anything to work on.未经测试的代码,因为您没有给我们任何工作。

# 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"]]

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

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