Question

I am a beginner. I was working with the Boston data set in the MASS package. I wanted to apply a filter to obtain the records where value of variable "chas" is 1 / TRUE and value of variable "age" is greater than 50.0

I tried :

> boston2<-subset(Boston, chas>0, age>50.0)
> boston2

The result I got was: data frame with 0 columns and 35 rows

However, I wanted all the records where chas is true and age is greater than 50 in a data frame.

What are the alternate methods I can use? And how can I extend the filtering to 3 / 4 / any number of variables.

Was it helpful?

Solution

You need to connect your different criteria using the various logical operators:

subset(Boston, chas > 0 & age > 50.0)

Read ?Logic to learn more.

The documentation for subset specifies that the second argument, subset, must be a single logical expression. When you separate them with commas, R interprets them as entirely different arguments, not as a single expression.

Your attempt is equivalent to:

subset(Boston, subset = chas > 0, select = age > 50.0)

so you get "all rows but no columns".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top