[R] Cannot subset a specific mean from this function
Jim Lemon
drjimlemon at gmail.com
Fri Dec 18 08:54:49 CET 2015
Hi Bradley,
I think I can see one or two reasons why you are getting creamed.
First, pollutantmean is a function, and there is no method for taking the
mean of a function.
Second, nitrate is unlikely to be an extractable element of pollutantmean
given the above code (that's the error).
I suspect that you have been asked to read the last 53 files of a set of
332.
When reading each one, extract the values of the element "nitrate" from
each dataframe that is created.
concatenate these "nitrate" values as you go, resulting in a vector of
nitrate concentration values
take the mean of that vector, probably removing any NA values
then return that mean value from the function.
While I'm not going to do the job for you, your assigned the names of the
files to the variable "files" and then tried to read from a variable named
"files_full". That won't work.
You then tried to merge the dataframes by adding each successive one to the
previous ones.
This will work, but you haven't returned the result from the function, or
assigned it to another variable.
You then ask for the structure of "pollutantmean" which is a function, not
a data frame
If you stick with the data frame accumulation, I suggest putting a:
return(dat)
as the last line of the function, and assigning it to something:
pollutant.df<-pollutantmean(...)
Then look at the structure of pollutant.df and if you are lucky, there will
be a "nitrate" element.
Jim
On Fri, Dec 18, 2015 at 3:06 PM, Bradley Wolf <chanmo5752 at gmail.com> wrote:
> HI,
> I am very new to R (and programming in general) and am taking a class in
> it now. I am getting creamed. I want the last part of the function
> statement below to give me the mean of the data set of nitrates. When I do
> it I get an error statement stating:
>
> Error in pollutantmean$nitrate :
> object of type 'closure' is not subsettable
>
> I am bringing in 332 records. I want the mean of nitrates for 52 of those
> records. Any suggestions would be helpful.
>
> pollutantmean <- function(specdata, nitrate,id = 1:332){
> files<-list.files(specdata, full.names = TRUE)
> dat<-data.frame()
> for (i in 280:332)
> dat <- rbind(dat, read.csv(files_full[i]))
> }
> str(pollutantmean)
> mean(pollutantmean$nitrate, na.rm = TRUE)
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list