I am working on a function that extract the contents of several CSV files and put in one data frame and then get the mean of a specific component of that data frame specified by the user
> airpollut <- function(pathway, pollutent, id = 1:322){
airpollutionfiles <- list.files(path = pathway, full.names = TRUE, pattern = ".csv")
totalvalues <- list()
for(i in id){
filereading <- read.csv(airpollutionfiles[i])
totalvalues[[i]] <- filereading
}
finaldata1 <- data.frame(rbind(totalvalues))
mean(finaldata1[[pollutent]])
}
but when I run the function I will get the error message:
> airpollut("finaldata", "sulfate")
[1] NA
Warning message:
In mean.default(finaldata1[[pollutent]]) :
argument is not numeric or logical: returning NA
so I tried to check out the output of that dataframe binding and I removed the mean function from the function I am creating and put head() instead:
airpollut <- function(pathway, pollutent, id = 1:322){
airpollutionfiles <- list.files(path = pathway, full.names = TRUE, pattern = ".csv")
totalvalues <- list()
for(i in id){
filereading <- read.csv(airpollutionfiles[i])
totalvalues[[i]] <- filereading
}
finaldata1 <- data.frame(rbind(totalvalues))
head(finaldata1)
}
the output is just the data stacked next to each other like a vector and my computer crushes. can you please tell me how to combine the rows from all the files? without using functions that are not in r base.
CodePudding user response:
A minimal reproducible example to facilitate understanding of the problem:
l <-list()
l[[1]] <- mtcars
l[[2]] <- mtcars
mean(as.data.frame(rbind(l))[[1]])
[1] NA
Warning message:
In mean.default(as.data.frame(rbind(l))[[1]]) :
argument is not numeric or logical: returning NA
You could use do.call
:
mean(do.call(rbind,l)[[1]])
[1] 20.09062