How to group specific responses into categories in R

So, in this dataframe, one of my variables "eco-share" has 4 possibilities "Economically inactive", "Full-time work", "Part-time work" and "Unemployed and work-related government training programs"

I want to group 3 of them together (Full Time Job, Part Time Job, and Unemployed and Work-Related Government Curriculum) so that I only get 2 opportunities “Inactive” or “Economically Active” ...

I was looking for ways to do this but couldn't find anything or could not apply to what I want to do.

+3


source to share


2 answers


If your class is a char variable, then you can simply create a new variable with a statement ifelse()

. If this is a factor, you should be careful and convert your type on the fly with as.characater.factor()

.



+1


source


If your vector is a factor, you can use the recode function in the Car library.



> library('car')
> x <- factor(rep(c("A", "B", "C" ,"D"), 3))
> x
 [1] A B C D A B C D A B C D
 Levels: A B C D
> recode(x, " c('A') = 'Economic Active'; c('B','C','D') = 'Economic 
  Inactive';")
  [1] Economic Active   Economic Inactive Economic Inactive Economic Inactive 
      Economic Active   Economic Inactive
  [7] Economic Inactive Economic Inactive Economic Active   Economic Inactive 
      Economic Inactive Economic Inactive
      Levels: Economic Active Economic Inactive

      

0


source







All Articles