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?
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.
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")
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:
write one condition with | (which means OR): filter(income2 == “Less than $10,000” | income2 == “Less than $50,000”)
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”))
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.
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 “ and ” 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: