我有一对夫妇的数据,其变量为:“家庭人数”,“户主”,“教育”,“收入”。 “家庭号”是唯一分配给每个家庭的ID号。 “户主”是指人是否是户主(1 =户主,2 =户主的配偶),“教育”和“收入”分别是受教育程度和个人收入。例如,数据如下所示。

'household_number'  'head_of_household'  'education'  'income'
        1                     1              high       1000
        1                     2              low        100
        3                     1              medium     500
        3                     2              high       800
        4                     2              high       800
        4                     1              high       800
        9                     1              low        150
        9                     2              low        200

我想为每个人创建配偶的变量。因此该数据如下所示。 “配偶教育”是配偶的教育水平,“配偶公司”是配偶的收入。
'household_number'  'head_of_household'  'education'  'income' 'spouse_edu' 'spouse_inc'
        1                     1              high       1000      low         100
        1                     2              low        100       high        1000
        3                     1              medium     500       high        800
        3                     2              high       800       medium      500
        4                     2              high       800       high        800
        4                     1              high       800       high        800
        9                     1              low        150       low         200
        9                     2              low        200       low         150

我的数据集非常大,因此我正在寻找简单的方法来做到这一点。有什么优雅的方法可以做到这一点吗?

以下是可复制的示例语法。
household_number <- c(1,1,3,3,4,4,9,9)
head_of_household <- c(1,2,1,2,2,1,1,2)
education <- c("high", "low", "medium", "high", "high", "high", "low", "low")
income <- c(1000, 100, 500, 800, 800, 800, 150, 200)

data <- data.frame(household_number, head_of_household, education, income)

最佳答案

您可以在此处使用base::revdplyr

library(dplyr)
data %>%
 group_by(household_number) %>%
 mutate(spouse_income = rev(income),
        spouse_education = rev(education)) %>%
 ungroup()

# A tibble: 8 x 6
#  household_number head_of_household education income spouse_income spouse_education
#             <dbl>             <dbl>    <fctr>  <dbl>         <dbl>           <fctr>
#1                1                 1      high   1000           100              low
#2                1                 2       low    100          1000             high
#3                3                 1    medium    500           800             high
#4                3                 2      high    800           500           medium
#5                4                 2      high    800           800             high
#6                4                 1      high    800           800             high
#7                9                 1       low    150           200              low
#8                9                 2       low    200           150              low

使用data.table的解决方案。
library(data.table)
setDT(data)[, c("spouse_income", "spouse_education") := .(rev(income), rev(education)),
            by = household_number][]

# same as
# setDT(data)[, `:=`(spouse_income = rev(income),
#                    spouse_education = rev(education)),
#             by = household_number][]

base R可以做到
transform(data,
          spouse_income = ave(income, household_number, FUN = rev),
          spouse_education = ave(education, household_number, FUN = rev))

关于r - 如何创建配偶变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47959420/

10-12 19:11