i am running this R code to import a lot of csv file and calculate one mean of a numerico vector (nitrate) for all the datasets filtering by some values of an integer vector, ID.
setwd("C:/Users/americo/Documents/Documenti_2_0/Education/Data science/Coursera/R programming/rworkingdir/specdata")
lista_tot1 = list.files(pattern="*.csv");
lista_tot2 = lapply(lista_tot1, read.csv, sep = ",")#stampa tutti i csv
lista_tot3<-do.call("rbind", lista_tot2)
lista_tot<-lista_tot3[is.element(lista_tot3$ID, 3:4),]
media<-mean(lista_tot$nitrate, na.rm=TRUE)
media
and it works.
But if i try to write this function, which seems similar to me:
pollutantmean <- function(directory, pollutant, id = 1:332) {
setwd(directory)
lista_tot1 = list.files(pattern="*.csv");
lista_tot2 = lapply(lista_tot1, read.csv, sep = ",")
lista_tot3<-do.call("rbind", lista_tot2)
lista_tot<-lista_tot3[is.element(lista_tot3$ID, id),]
media<-mean(lista_tot$pollutant), na.rm=TRUE)
return(media)
}
and then
media1<-pollutantmean("C:/Users/americo/Documents/Documenti_2_0/Education/Data science/Coursera/R programming/rworkingdir/specdata","nitrate", 3:4)
it then returns to me the message:
warning message in mean.default argument is not numeric or logical returning na
...but my vector is numeric
that really doesn't make sense to me. if i ask to return of the structure of the last dataset, lista_tot, nitrate is still numeric.
I really don't know what to do, if some very experienced user could help me i would be extremely grateful.
Change this:
media<-mean(lista_tot$pollutant), na.rm=TRUE)
to this:
media<-mean(lista_tot[[pollutant]], na.rm=TRUE)
because the "$" does not evaluate it's argument. In this case "pollutant" is not a column name so you need an extraction function that will evaluate what the named object, pollutant
, is "carrying" (namely the column name, "nitrate")to get the proper extraction from the dataframe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With