Converting likert scale variables to be dichotomous

I'm trying to perform a latent class analysis at the moment on a dataset. Most of the variables in the dataset are coded 1 to 10 on a likert scale e.g. 1 is least and 10 is most. I want to convert these variables so that 1:5 are grouped and labelled as 1 and then 6:10 are grouped and labelled as 2. I'm not sure if there is code that can do this?

I have tried recode(variable$data) but that does not seem to work.

Is there a command that I can use to recode the values?

Hi! The case_when function of the dplyr package is probably the most efficient to recode variables.

As @ihecker mentioned, you can do something like the below:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

x <- 1:10



dplyr::case_when(
  x <= 5 ~ 1,
  x >= 6 ~ 2
)
#>  [1] 1 1 1 1 1 2 2 2 2 2

Created on 2021-04-23 by the reprex package (v0.3.0)

Thank you! Will the variable stay coded like this for later analysis with the coded variable?

Here is an example of how you will retain it. You can call mutate which will then change or create a new column in your data frame. Here you see we create z based on column_a.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- data.frame(
"column_a" = 1:10,
"column_b" = 2:11
)

df <- df %>% 
  mutate(z =
dplyr::case_when(
  column_a <= 5 ~ 1,
  column_a >= 6 ~ 2
)
)

df
#>    column_a column_b z
#> 1         1        2 1
#> 2         2        3 1
#> 3         3        4 1
#> 4         4        5 1
#> 5         5        6 1
#> 6         6        7 2
#> 7         7        8 2
#> 8         8        9 2
#> 9         9       10 2
#> 10       10       11 2

Created on 2021-04-23 by the reprex package (v0.3.0)

Thanks again, is there any way to add to this code to make values >10 appear as NA?

Here you go

library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.0.5
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- data.frame(
  "column_a" = 1:12,
  "column_b" = 2:13
)

df <- df %>% 
  mutate(z =
           dplyr::case_when(
             column_a <= 5 ~ 1,
             column_a >= 6 & column_a <= 10  ~ 2,
             column_a > 10 ~ NA_real_
           )
  )


df
#>    column_a column_b  z
#> 1         1        2  1
#> 2         2        3  1
#> 3         3        4  1
#> 4         4        5  1
#> 5         5        6  1
#> 6         6        7  2
#> 7         7        8  2
#> 8         8        9  2
#> 9         9       10  2
#> 10       10       11  2
#> 11       11       12 NA
#> 12       12       13 NA

Created on 2021-04-23 by the reprex package (v0.3.0)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.