使用R中的重新编码产生两个新变量?

问题描述 投票:2回答:1

我逐渐习惯于在R中重新编码变量,但是在创建两个新变量时遇到了一些麻烦。例如,我尝试了以下方法:

income2018$income2 <- dplyr::recode(income2018$income, '51' = 1L, '52' = 1L, '53' = 2L)
income2018$income3 <- dplyr::recode(income2018$income, '57' = 1L, '58' = 1L, '50' = 2L)

看起来值似乎没有正确地应用于新变量。

这是我尝试重新创建的SPSS语法:

RECODE income (51,52=1)(53=2) into income2
RECODE income (57,58=1)(50=2) into income3

非常感谢您的协助。

非常感谢。

r spss
1个回答
0
投票

看来您可能需要稍微重新排列代码,但是如果没有reprex,很难分辨出来>

您可能想要尝试:

income2018 <- income2018 %>%
dplyr::mutate(income2 = income) %>%
dplyr::recode(income2, '51' = 1L, '52' = 1L, '53' = 2L)
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.