how can I group two different categories within the same variable?

Hi, I´m doing a homework for my college. I have a sheet with regions, years and income. I need to group all the income of North of 2011. When I try to make a varaible with two categories, the RStudio say to me that´s impossible! Someone knows how I can solve this problem?
Thank you so much.
(Sorry for my bad English, it´s my second language.)

I try to do this:
The sheet that I have is a .RData
incomeNORTH2011<- ADAposentado$INCOME[ADAposentado$YEAR=="2011"[ADAposentado$REGION=="North"]]

Welcome, @ana_pais! First of all, it is going to be easier for folks around here to help if you include a sample of your data as well as the code you've used to get to where you are stuck. Without knowing the structure of the data or what you've already tried, it's very hard to help.

See this post for more information about best practices for writing questions and including a reproducible example (reprex).

1 Like

@ana_pais As suggested by @mfherman, it will be good to have a sample data and a code that you are using in order to replicate the error and try having a solution.

From what you have described in your problem statement, perhaps aggregate is the function that you are looking for. You can specify if you want a total sum or a mean income for each of your groups.

Let me know if this helps!


If I understand you correctly you just want to extract all INCOME values from ADAposentado where YEAR is "2011" and REGION is "North". If so, you're almost there!

incomeNORTH2011 <- ADAposentado$INCOME[ADAposentado$YEAR=="2011" & ADAposentado$REGION=="North"]

The ampersand ("&") is the vectorized AND operator. Do not confuse with the short-circuit AND operator "&&".

Good luck with your homework!



Thank you, @heramb ! :wink:

OMG thank you so much !!!! It works perfectly @stkrog :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.