'Combine or merge values

I would like to combine/merge my values into each other and become one value in R. For instance, to combine 1+3, 5+6, 10+11, 12+13. Does anyone know how to do that? :-)

tibble::tibble(
  Educational_level = c(1, 3, 5, 6, 10, 11, 12, 13)

This is what I have tried, but it do not merge the factors that I would like when I run the linear regression.

ess7no <- ess7no %>% mutate(edlvdno = as_factor(edlvdno)) %>% mutate(edlvdno = recode(edlvdno, "1" = "3" , "5" = "6", "10" = "11", "12" = "13"))



Solution 1:[1]

df <- tibble(
  Educational_level = c(1, 3, 5, 6, 10, 11, 12, 13),
  Labels = c(
    "Not graduated", "Primary school", "High school", "High school",
    "Bachelor", "Bachelor", "Master", "Master"
  )
)
library(dplyr)
df %>%
  mutate(Labels = ifelse(Labels %in% c(
    "Not graduated",
    "Primary school"
  ), stringr::str_c("Not graduated", "_", "Primary school"), Labels)) %>%
  group_by(Labels) %>%
  summarise(Educational_level = sum(Educational_level))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julian