Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, mutate and "Unsupported type NILSXP for column"

Tags:

r

dplyr

Here's a snapshot of my data:

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

I'm trying to use the twinfam(twin in family) and SAMESEX(first two born children same sex) binary variables to create a fourth variable that takes on 4 possible values:

  • 1 if SAMESEX == 0 & twinfam == 0

  • 2 if SAMESEX == 1 & twinfam == 0

  • 3 if SAMESEX == 0 & twinfam ==1

  • 4 if SAMESEX == 1 & twinfam ==1

After playing around a bit I tried using:

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

but I get the error:

Error: Unsupported type NILSXP for column "both"

and can't seem to resolve this error. Any advice on why I get this error, and how it might be resolved, would be appreciated!

like image 759
Milhouse Avatar asked Aug 27 '16 16:08

Milhouse


1 Answers

It is better to create a key/value dataset and do a 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
like image 190
akrun Avatar answered Sep 23 '22 02:09

akrun