【问题标题】:Creating dummy variable based on group properties根据组属性创建虚拟变量
【发布时间】:2016-12-12 20:48:01
【问题描述】:

我的数据如下所示:

ID   CSEX    MID   CMOB   CYRB   1ST   2ND       
1    1       1     1      1991   0     1
2    1       1     7      1989   1     0
3    2       2     1      1985   1     0
4    2       2     11     1985   0     1
5    1       2     9      1994   0     0
6    2       3     4      1992   1     0
7    2       4     2      1992   0     1
8    1       4     10     1983   1     0

ID = 孩子 ID,CSEX = 孩子性别,MID = 母亲 ID,CMOB = 出生月份,CYRB = 出生年份,第一个 = 第一个出生假人,第二个 = 第二个出生假人。

如果家庭中出生的前两个孩子(即具有相同的 MID)是相同性别,我正在尝试创建一个取值为 1 的虚拟变量。

我试过了

 Identifiers_age <- Identifiers_age %>% group_by(MPUBID) %>% 
    mutate(samesex = 
            as.numeric(((first == 1 & CSEX == 1) & (second == 1 & CSEX == 1)) 
                       | (first == 1 & CSEX == 2) & (second == 1 & CSEX ==2))))

但很明显,这仍然只检查每个单独 ID 的条件,而不是按 MID,因此返回一个总是取值 = 0 的虚拟对象。

谢谢

编辑预期输出:

ID   CSEX    MID   CMOB   CYRB   1ST   2ND   SAMESEX 
1    1       1     1      1991   0     1     1
2    1       1     7      1989   1     0     1
3    2       2     1      1985   1     0     1
4    2       2     11     1985   0     1     1
5    1       2     9      1994   0     0     1
6    2       3     4      1992   1     0     0
7    2       4     2      1992   0     1     0 
8    1       4     10     1983   1     0     0

即对于在前两个孩子是同性的家庭中的任何个人,虚拟 SAMESEX = 1

Edit2(我之前展示的只是我做的一个例子,因为真正的数据集调用结构给出了):

     CPUBID MPUBID  CSEX  CMOB  CYRB  first second 
     <int>  <int> <int> <int> <int>   <dbl>  <dbl>   
1     201      2     2     3  1993     1      0   
2     202      2     2    11  1994     0      1      
3     301      3     2     6  1981     1      0       
4     302      3     2    10  1983     0      1      
5     303      3     2     4  1986     0      0       
6     401      4     1     8  1980     1      0       
7     403      4     2     3  1997     0      1       
8     801      8     2     3  1976     1      0       
9     802      8     1     5  1979     0      1       
10    803      8     2     9  1982     0      0       

和str:

 Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 11512 obs. of  7 variables:
 $ CPUBID : int  201 202 301 302 303 401 403 801 802 803 ...
 $ MPUBID : int  2 2 3 3 3 4 4 8 8 8 ...
 $ CSEX   : int  2 2 2 2 2 1 2 2 1 2 ...
 $ CMOB   : int  3 11 6 10 4 8 3 3 5 9 ...
 $ CYRB   : int  1993 1994 1981 1983 1986 1980 1997 1976 1979 1982 ...
 $ first  : num  1 0 1 0 0 1 0 1 0 0 ...
 $ second : num  0 1 0 1 0 0 1 0 1 0 ...

【问题讨论】:

    标签: r dplyr dummy-variable


    【解决方案1】:

    这可能有帮助

    library(dplyr)
    Identifiers_age %>%
              group_by(MID) %>% 
              mutate(ind1 = CSEX *`1ST`,
                     ind2 = CSEX *`2ND`, 
                     SAMESEX = as.integer(n_distinct(c(ind1[ind1!=0], 
                                 ind2[ind2!=0]))==1 &  sum(ind1) >0 & sum(ind2) > 0)) %>% 
                    select(-ind1, -ind2)
    #     ID  CSEX   MID  CMOB  CYRB   1ST   2ND SAMESEX
    #  <int> <int> <int> <int> <int> <int> <int>   <int>
    #1     1     1     1     1  1991     0     1       1
    #2     2     1     1     7  1989     1     0       1
    #3     3     2     2     1  1985     1     0       1
    #4     4     2     2    11  1985     0     1       1
    #5     5     1     2     9  1994     0     0       1
    #6     6     2     3     4  1992     1     0       0
    #7     7     2     4     2  1992     0     1       0
    #8     8     1     4    10  1983     1     0       0
    

    或者它可以稍微紧凑

    Identifiers_age %>%
             group_by(MID) %>%
             mutate(SAMESEX = as.integer(n_distinct(c(CSEX * NA^!`1ST`, CSEX * NA^!`2ND`), 
                            na.rm = TRUE)==1 & sum(`1ST`) > 0 & sum(`2ND`) > 0))
    

    数据

    Identifiers_age <- structure(list(ID = 1:8, CSEX = c(1L, 1L, 2L, 2L, 1L, 
     2L, 2L, 
     1L), MID = c(1L, 1L, 2L, 2L, 2L, 3L, 4L, 4L), CMOB = c(1L, 7L, 
     1L, 11L, 9L, 4L, 2L, 10L), CYRB = c(1991L, 1989L, 1985L, 1985L, 
     1994L, 1992L, 1992L, 1983L), `1ST` = c(0L, 1L, 1L, 0L, 0L, 1L, 
     0L, 1L), `2ND` = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L)), .Names = c("ID", 
     "CSEX", "MID", "CMOB", "CYRB", "1ST", "2ND"), class = "data.frame",
     row.names = c(NA, -8L))
    

    【讨论】:

    • 感谢您的回复!当我在实际数据集中实现这一点时,变量 SAMESEX 似乎只取值 0,我将继续使用它,看看是否可以让它工作
    • @Milhouse 请检查您数据的str。我更新了我正在使用的数据。
    • 我又编辑了,是这个意思吗?很抱歉有任何困惑
    • @Milhouse 对于您的新数据集,我也没有得到全 0,即假设我们有 'df1' 即df1 %&gt;% group_by(MPUBID) %&gt;% mutate(ind1 = CSEX * first, ind2 = CSEX *second, SAMESEX = as.integer(n_distinct(c(ind1[ind1!=0], ind2[ind2!=0]))==1 &amp; sum(ind1) &gt;0 &amp; sum(ind2) &gt; 0)) %&gt;% .$SAMESEX #[1] 1 1 1 1 1 0 0 0 0 0
    • @Milhouse 你还加载了plyr 吗?在这种情况下,您可能需要dplyr::mutate(ind1 = CSEX* first, ...
    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2017-08-01
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多