r语言 - 组合或合并值



我想将我的值相互组合/合并,成为r中的一个值。例如,将1+ 3,5 + 6,10 + 11,12 +13组合。有人知道怎么做吗?: -)

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

这是我尝试过的,但是当我运行线性回归时,它并没有合并我想要的因素。

ess7no <- ess7no %>%突变(edlvdno = as_factor(edlvdno)) %>%突变(edlvdno = recode(edlvdno, "1")="3";,"5";= "6";= "11";="13")

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))

最新更新