i am trying to do an assignment from the R programming course at coursera, but my function does not work, and i have no idea why. i see no problems with it, and Rstudio reports no errors as well.
this is the function
pollutantmean<- function(id, polutant) {
files<- list.files("specdata", full.names = T)
table<-data.frame()
for(id in head(id,n=1):tail(id,n=1)){
loc<-read.csv(files[id])
table<- rbind(table,loc)
}
if(polutant=="nitrate") {
mean(table[,"nitrate"],na.rm = T )
}
if(polutant=="sulfate") {
mean(table[,"sulfate"],na.rm = T )
}
}
but when i try to run
pollutantmean(8,"nitrate")
or anything similar, nothing happens. i see no output or change in the global environment.
but when i run the statements without putting them in the function, line for line, it works
if i do this:
id<- 5
then
polutant<-"nitrate"
then
files<- list.files("specdata", full.names = T)
then
table<-data.frame()
then
for(id in head(id,n=1):tail(id,n=1)){
loc<-read.csv(files[id])
table<- rbind(table,loc)
}
(here, i would already see table and doc merging in the global environment)
if(polutant=="nitrate") {
mean(table[,"nitrate"],na.rm = T )
}
here, i would get an answer that matches the example output provided with the asignment.