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

0g0grzrc  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(101)

我在阅读/写入CSV文件时遇到了很多麻烦。假设我在一个文件夹中有300多个CSV文件,每个CSV文件都是一个值矩阵。
如果我想找出每个CSV文件的特征,例如哪些行的确切数字是3,并将结果写入每个测试的另一个CSV文件,我将如何对300个不同的CSV文件进行迭代呢?
例如,假设我为每个文件运行以下代码:

values_4 <- read.csv(file = 'values_04.csv', header=FALSE)  // read CSV in as it's own DF
values_4$howMany3s <- apply(values_04, 1, function(x) length(which(x==3))) // compute number of 3's 
values_4$exactly4 <- apply(values_04[50], 1, function(x) length(which(x==4))) // show 1/0 on each column that has exactly four 3's 
values_4 // print new matrix

然后我不断地复制和粘贴这些代码,将“4”改为5、6等,并记录这些值。这对我来说似乎效率很低,但我在R方面没有足够的经验来知道我的选择是什么。我是否应该考虑将所有300个CSV文件添加到一个列表中,并以某种方式循环使用它们?
感谢您的帮助!

v09wglhw

v09wglhw1#

这里有一种方法,你可以阅读所有的文件,并处理他们。未经测试的代码,因为你还没有给我们任何工作。

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

相关问题