How do I filter what I see in my summary?


#1

Hello,

Please kindly help.
I want to show only 2 income types “Less than $10,000” and “$75,000 or more”. I tried using filter but it does not work for me. (Error in group_vars(x) : object ‘income2’ not found) when using filter(income2 == “Less than $10,000”)
What do I do in order to only show the two income levels I am interested in and not all of them?

Thanks in advance,
Christine

brfss2013 %>%

count(income2, genhlth) %>%

group_by(income2) %>%

mutate(prop = n / sum(n)) 


#2

Hi Christine,

Are you chaining the filter statement at the end of the code you’ve included below (it would look ~like the code I’ve written)?

brfss2013 %>%
count(income2, genhlth) %>%
group_by(income2) %>%
mutate(prop = n / sum(n))  %>%
filter(income2 == "Less than $10,000")

Also, I don’t know if you’ve taken a look at the reprex package yet, but it essentially helps you make an example of the problem you’re having so that someone else can run it themselves to help you troubleshoot. It’s pretty awesome, and definitely worth getting acquainted with for asking questions on here.

@njtierney wrote a great post with gifs about it on his blog: http://www.njtierney.com/post/2017/01/11/magic-reprex/


#3

Hi Mara,

Nope. Thank you I learnt something, it works perfect now.


#4

But what if I want 2 income levels, how do I write that? It does not work with ; filter(income2 == “Less than $10,000”, income2 == “Less than $50,000”)


#5

You can do that using the or operator (|), or you can put the conditions together into a vector and use %in%. For example:

conditions <- c("Less than $10,000", "Less than $50,000")

Then, in your filter line you’d use

filter(income2 %in% conditions)

The “Useful filter functions” section of the dplyr docs is short, and helpful!
http://dplyr.tidyverse.org/reference/filter.html#useful-filter-functions


#6

I am doing it wrong…

brfss2013 %>%
count(income2, genhlth) %>%
group_by(income2) %>%
mutate(prop = n / sum(n))  %>%
conditions <- c("Less than $10,000", "Less than $50,000") %>%
filter(income2 %in% conditions)

Error in UseMethod(“filter_”) : no applicable method for ‘filter_’ applied to an object of class “character”


#7

Hi,
When you separate conditions in filter with a dot, it is equivalent to AND, so it is normal that you have no values because your variable is not equal to “Less than $10,000” and “Less than $50,000” at the same time.
You have 2 options to view your 2 levels:

  1. write one condition with | (which means OR): filter(income2 == “Less than $10,000” | income2 == “Less than $50,000”)

  2. Use the %in% operator that checks if a value is in a vector : filter(income2 %in% c(“Less than $10,000”, “Less than $50,000”))


#8

Yeah, sorry about that. The conditions vector isn’t part of the “chain.” That’s a separate vector that you’d create before executing everything else. The ways that @FlorianGD outlined will both work, but the one with the %in% relies on the creation of the conditions vector beforehand.


#9

Thank you so much that is indeed logic. But I am afraid I am STILL not doing it right…

brfss2013 %>%
count(income2, genhlth) %>%
group_by(income2) %>%
mutate(prop = n / sum(n))  %>%
filter(income2 == “Less than $10,000” | income2 == “Less than $50,000”)

Error: unexpected input in:
“mutate(prop = n / sum(n)) %>%
filter(income2 == �”


#10

That’s an encoding error. When you cut and paste from Discourse (or anything on the internet, really) sometimes you’ll get auto-formatting on the characters that you don’t want (in this case, I’m guessing it’s the quotation marks, which go from " to “word” (see how they’re curly?) automatically on here (so, when you paste them they essentially read as &ldquo; and &rdquo; rather than ".

Short of the long— type the statement in directly!

brfss2013 %>%
count(income2, genhlth) %>%
group_by(income2) %>%
mutate(prop = n / sum(n))  %>%
filter(income2 == "Less than $10,000" | income2 == "Less than $50,000")

Here’s a picture of the difference when rendered on here:


#11

I am cursed, I am still getting errors…

36


#12

Sorry please disregard my last message, it is working perfect, I forgot to take away a bracket!! Thank you!!!