[R] Problem with assignment 1 part 1
John McKown
john.archie.mckown at gmail.com
Mon Aug 11 16:46:51 CEST 2014
On Mon, Aug 11, 2014 at 9:00 AM, michelle maurin
<michimaurin at hotmail.com> wrote:
> see code below
>
>
> pollutantmean <- function(directory, pollutant, id = 1:332) {
> files_list <- list.files(directory, full.names=TRUE) #creates a list of
> files
> dat <- data.frame()#creates an empty data frame
> for (i in 1:332) {
> dat <- rbind(dat, read.csv(files_list[i]))#loops through the files,
> rbinding them together
> }
>
> #subsets the rows that match the 'pollutant' argument
> median(dat_subset$pollutant, na.rm=TRUE) #identifies the median of the
> subset
> }
>
>
>
> ##I highlighted the area that I think has the problem , I helped my self
> using the tutorial found on the forum ,for assignment 1
I really think your not where you believe you are. This is an email
list for general questions on the R language. I am not aware of an
"the tutorial found on the forum". But I do think that I have an idea
of what your problem is. Basically you want to find all the rows in
"dat" which have a pollutant (dat$pollutant) of either "sulfate" or
"nitrate". The which() function isn't going to do that for you. The
which() function takes a logical vector of TRUE and FALSE values. It
return an integer vector which has the index values of the TRUE
entries. For example:
> which(c(TRUE,FALSE,FALSE,TRUE,FALSE,TRUE))
[1] 1 4 6
I realise how this can be thought of as how to do this. And if could
work, but is unnecessary in this case. But the real problem is the
segment:
dat["suflate","nitrate"] == pollutant
If you would try this (I can't because I don't have the data files),
you would see that this is not asking the right question. You want to
see if dat$pollutant is either "suflate" or "nitrate". Or, expanding a
bit you want to ask: 'is dat$pollutant equal to "suflate"? If not, is
it equal to 'nitrate"?'. The answer to this question will be the
proper logical vector that you can either use in the which() function,
or directly as a row selector. The hint on how to ask this question is
to use the ifelse() function properly.
So your line (with the critical method of the proper use of ifelse)
should look something like:
dat_subset <- dat[which(ifelse(????),];
#or, equivalently
dat_subset <- dat[ifelse(???),];
This latter is valid because the R language will accept a logical
vector as a "selector" and only return the data where the logical
value is TRUE.
I am deliberately leaving the challenge of how to use the ifelse() for
you. Remember, from the documentation, that the form of the ifelse()
is: ifelse(condition,result-if-condition-true,result-if-condition-false.)
Hopefully this is a sufficient clew to get you going.
I won't comment on the rest of the code because I don't know the
problem. Or what "forum" you're talking about.
>
> Best regards
>
>
>
> Michelle
>
>
>
> ________________________________
> Date: Sun, 10 Aug 2014 22:06:38 -0500
> Subject: Re: [R] Problem with assignment 1 part 1
> From: john.archie.mckown at gmail.com
> To: michimaurin at hotmail.com
> CC: r-help at r-project.org
>
> What code.
>
> Also, the forum has a "no homework" policy. Your subject implies this is
> homework, so you might not get any answers. You might get a hint or two
> though.
>
> On Aug 10, 2014 10:00 PM, "michelle maurin" <michimaurin at hotmail.com> wrote:
>
> I think my code is very close I can seem to be able to debug it Might be
> something very simple I know the problem is on the last 3 lines of code can
> you please help?
> Thanks
> Michelle
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan
Maranatha! <><
John McKown
More information about the R-help
mailing list