这是我的数据快照:

  structure(list(CPUBID = c(1000001L, 1000002L, 1000003L, 10001L,
1000201L, 1000203L, 10003L, 1000801L, 1000802L, 1000803L, 1001L,
1001101L, 1001102L, 1001601L, 1002401L, 1002402L, 1002403L, 1002601L,
1002602L, 1002604L), MPUBID = c(10000L, 10000L, 10000L, 100L,
10002L, 10002L, 100L, 10008L, 10008L, 10008L, 10L, 10011L, 10011L,
10016L, 10024L, 10024L, 10024L, 10026L, 10026L, 10026L), CYRB = c(1982L,
1984L, 1988L, 1985L, 1986L, 1992L, 1993L, 1984L, 1986L, 1988L,
1983L, 1987L, 1992L, 1977L, 1981L, 1984L, 1998L, 1980L, 1981L,
1984L), twinfam = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), SAMESEX = c(1L, 1L, 1L,
1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
0L), top25 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,
0, 0, 0, 0), top5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0), quantity = c(1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1)), .Names = c("CPUBID", "MPUBID",
"CYRB", "twinfam", "SAMESEX", "top25", "top5", "quantity"), row.names = c(NA,
20L), class = "data.frame")

我正在尝试使用twinfam(家庭中的双胞胎)和SAMESEX(前两个同性出生的 child )二进制变量来创建第四个变量,该变量采用4个可能的值:
  • 1,如果SAMESEX == 0&twinfam == 0
  • 2,如果SAMESEX == 1&twinfam == 0
  • 3,如果SAMESEX == 0且twinfam == 1
  • 4,如果SAMESEX == 1&twinfam == 1

  • 玩了一段时间后,我尝试使用:
    df <- df %>% mutate(both = for (i in 1:nrow(PIATmathreg6)) {
                                  if(twinfam[i] == 0 & SAMESEX[i] == 0) both = 1
                                  else if(twinfam[i] == 0 & SAMESEX[i] == 1) both = 2
                                  else if(twinfam[i] == 1 & SAMESEX[i] == 0) both = 3
                                  else both = 4})
    

    但是我得到了错误:
    Error: Unsupported type NILSXP for column "both"
    

    并且似乎无法解决此错误。对于为什么会出现此错误以及如何解决该错误的任何建议,将不胜感激!

    最佳答案

    最好创建一个键/值数据集并执行left_join

    library(dplyr)
    df2 <- data.frame(SAMESEX = c(0, 1, 0, 1), twinfam = c(0, 0, 1, 1), both = 1:4)
    left_join(df, df2, by = c("SAMESEX", "twinfam"))
    #    CPUBID MPUBID CYRB twinfam SAMESEX top25 top5 quantity both
    #1  1000001  10000 1982       0       1     0    0        1    2
    #2  1000002  10000 1984       0       1     0    0        1    2
    #3  1000003  10000 1988       0       1     0    0        1    2
    #4    10001    100 1985       0       1     0    0        1    2
    #5  1000201  10002 1986       0       0     0    0        1    1
    #6  1000203  10002 1992       0       0     1    0        1    1
    #7    10003    100 1993       0       1     0    0        1    2
    #8  1000801  10008 1984       0       0     0    0        1    1
    #9  1000802  10008 1986       0       0     0    0        1    1
    #10 1000803  10008 1988       0       0     0    0        1    1
    #11    1001     10 1983       0       1     1    0        0    2
    #12 1001101  10011 1987       0       0     0    0        0    1
    #13 1001102  10011 1992       0       0     0    0        0    1
    #14 1001601  10016 1977       0       1     0    0        1    2
    #15 1002401  10024 1981       0       0     1    0        1    1
    #16 1002402  10024 1984       0       0     0    0        1    1
    #17 1002403  10024 1998       0       0     0    0        1    1
    #18 1002601  10026 1980       0       0     0    0        1    1
    #19 1002602  10026 1981       0       0     0    0        1    1
    #20 1002604  10026 1984       0       0     0    0        1    1
    

    关于R,mutate和 "Unsupported type NILSXP for column",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39183359/

    10-10 11:40