R循环对多个csv文件执行功能 [英] R loop perform function on multiple csv files
问题描述
我已经尝试创建一个for循环,为4个csv文件中的每个文件类似,但是有更多的文件。
I have tried to create a for loop that does something for each of 4 csv files similar to this but with more files.
dat1<- read.csv("female.csv", header =T)
dat2<- read.csv("male.csv", header =T)
for (i in 1:2) {
message("Female, Male")
Temp <- dat[i][(dat[i]$NAME == "Temp"), ]
Temp <- Temp[complete.cases(Temp)]
print(mean(Temp$MEAN))
b $ b
但是,我得到一个错误:
However, I get an error:
错误:$ MEAN:$ operator对原子向量无效
Error in Temp$MEAN : $ operator is invalid for atomic vectors
不知道为什么这不工作。
Not sure why this isn't working. Any help would be appreciated for looping through csv files!
推荐答案
我个人认为,最简单的方法是使用plyr package:
Personally, I think the easiest way to do this is with the plyr package:
library(plyr)
myFiles <- c("male.csv", "female.csv")
dat <- ldply(myFiles, read.csv)
dat <- dat[complete.cases(dat), ]
mean(dat$MEAN)
这种方式的工作原理是首先创建一个文件名的向量。然后ldply()函数对文件名的向量执行函数read.csv(),并将输出自动转换为data.frame。
The way this works is that you first create a vector of file names. Then the ldply() function performs the function read.csv() on the vector of filenames, and converts the output automatically to a data.frame. Then you do the complete.cases() and mean() in the usual way.
编辑:
但是如果你想要每个文件的平均值,这里是一个方法:
But if you want the mean of each file then here is one way of doing it:
# create a vector of files
myFiles <- c("male.csv", "female.csv")
# create a function that properly handles ONLY ONE ELEMENT
readAndCalc <- function(x){ # pass in the filename
tmp <- read.csv(x) # read the single file
tmp <- tmp[complete.cases(tmp), ] # complete.cases()
mean(tmp$MEAN) # mean
}
x <- "male.csv"
readAndCalc(x) # test with ONE file
sapply(myFiles, readAndCalc) # run with all your files
你首先创建一个文件名的向量,就像以前一样。然后创建一个只处理一个文件的函数。然后你可以使用刚刚创建的readAndCalc函数来测试函数是否工作。最后,使用sapply()函数为所有文件执行此操作。希望有帮助。
The way this works is that you first create a vector of filenames, just like before. Then you create a function that processes ONLY ONE file at a time. Then you can test that the function works using the readAndCalc function you just created. Finally do it for all your files with the sapply() function. Hope that helps.
这篇关于R循环对多个csv文件执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!