Recode categorical variable

#1

Hey,
I am new to R and need some help.
I want to recode categorical variable. I want category 1 and 2 to be in one category 0 with a name "no access", similarly category 3, 4, and 5 to be 1 with a name "with access". Other categories should be NA. Here is the code I have in Stata:

q6001 (1/2=0 "No access")(3/5=1 "With access")(6/max=.), gen(q6001BR)

Thanks in advance

0 Likes

#2

In R there is no label attribute for factors (categorical variables) labels get converted to levels (categories), so you may want to just use your labels as levels like this.

library(car)

x <- as.factor(c(1, 2, 3, 4, 5, 6, 7))
recode(x, "1:2='no access';3:5='with access';else=NA")
#> [1] no access   no access   with access with access with access <NA>       
#> [7] <NA>       
#> Levels: no access with access

Created on 2019-03-03 by the reprex package (v0.2.1)

1 Like

#3

Hi,

Thanks for your help (andresrcs) but unfortunately I am still stuck with the problem.

I have categories variable that has 8 categories to 2 categories (0 and 1)by using the following codes:

da31381.0002$Q6008BR <- da31381.0002$Q6008
levels(da31381.0002$Q6008BR) <- c("0", "0", "1", "1", "1", NA,NA,NA)

summary(da31381.0002$Q6001BR)
0 1 NA's
14149 424 477

But in structure R mention it 1 and 2.
str(da31381.0002$Q6001BR)

Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...

I need to change it to 0 and 1 so that its structure also changes to 0 and 1

Later on I need to change the variable(Q6001BR) to numeric to rowSum it with other similar variables. With above problem the sum results in summing the values as shown by the str() function.

Your help will be highly appreciated.Thanks

0 Likes

#4

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

Maybe a walk around solution would be to use a boolean variable (TRUE/FALSE = 1/0) instead of a factor that way you can use rowsum directly.

0 Likes

#5

I'm not sure, but it sounds as if you have an 8-category, non-numeric variable and want a numeric binary. If so ...

library(tidyverse)
x <- as.factor(c(1, 2, 3, 4, 5, 6, 7,8)) %>% as_tibble() %>% 
 rename(MyBinaryVar=value) %>% 
 mutate(MyBinaryVar = dplyr::case_when(MyBinaryVar %in% 1:3 ~ 0, 
                                       MyBinaryVar %in% 4:5 ~ 1, 
                                       TRUE ~ NA_real_))
x

A tibble: 8 x 1

MyBinaryVar

1 0
2 0
3 0
4 1
5 1
6 NA
7 NA
8 NA

0 Likes