Skip to content

Problem with assignment 1 part 1

1 message · John McKown

#
On Mon, Aug 11, 2014 at 9:00 AM, michelle maurin
<michimaurin at hotmail.com> wrote:
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:
[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.