R创建变量IF ELSE导致错误的值

时间:2014-04-03 14:35:47

标签: r if-statement

我有一个数据框: “连续”家庭的数量,每个家庭的成员数量可变“头,配偶,父母和子女或孙子”和“nchild”中的孩子总数

我想创建一个新变量(在dput中我为了清晰起见添加了一个例子:withCM'与男孩一起生活'和withCF)。我尝试了各种组合,但是我不能在相同的“序列”中区分孩子的性别,因此只有当涉及= =“孩子”和性别== 1时,对于withCM = 1,但是1会出现在不同的行(头部,配偶或父母的行)

mydata$withCM<- ifelse(mydata$nchild>0&mydata$relate!="child",1,0)

mydata <- structure(list(serial = c(12345L, 12345L, 12345L, 12345L, 12346L, 
12346L, 12347L, 12347L, 12347L, 12348L, 12348L, 12348L, 12348L, 
12348L, 12348L, 12348L, 12349L, 12350L, 12350L, 12351L, 12351L, 
12351L, 12352L, 12352L, 12352L, 12352L, 12352L, 12353L, 12354L, 
12354L), age = c(45L, 44L, 13L, 11L, 29L, 28L, 65L, 61L, 35L, 
68L, 61L, 35L, 34L, 6L, 2L, 1L, 62L, 54L, 52L, 67L, 67L, 12L, 
49L, 50L, 28L, 21L, 22L, 70L, 89L, 55L), sex = c(1L, 2L, 2L, 
1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), relate = structure(c(4L, 
7L, 1L, 1L, 4L, 7L, 6L, 6L, 4L, 4L, 7L, 1L, 2L, 3L, 3L, 3L, 4L, 
4L, 7L, 4L, 7L, 3L, 4L, 7L, 1L, 5L, 5L, 4L, 6L, 4L), .Label = c("child", 
"childinlaw", "grandchild", "head", "nonrelative", "parent", 
"spouse"), class = "factor"), nchild = c(2L, 2L, 0L, 0L, 0L, 
 0L, 1L, 1L, 0L, 1L, 1L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L), conhija = c(1L, 1L, 0L, 
 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), conhijo = c(1L, 
 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 
 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("serial", 
 "age", "sex", "relate", "nchild", "conhija", "conhijo"), class = "data.frame", row.names = c(NA, 
 -30L))

2 个答案:

答案 0 :(得分:1)

您可以将性别,家庭和家庭内角色制表为:

xtab <- table(mydata$serial, mydata$sex, mydata$relate)

然后选择家庭的负责人(或者在评论栏中,选择具有特定关系的任何人),并按如下方式更改他们的标签:

mydata$sex1 <- 0
mydata$sex2 <- 0
ind <- mydata$relate=="head"
#ind <- mydata$relate %in% c("head","spouse","parent")
mydata$sex1[ind] <- xtab[as.character(mydata$serial[ind]), "1", "child"]
mydata$sex2[ind] <- xtab[as.character(mydata$serial[ind]), "2", "child"]

答案 1 :(得分:1)

使用lapply分成家庭,然后测试他们是否是成年人,并且该单元中至少有一名男孩。

lives_with_boy <- function(serial)
{
  unit <- mydata[mydata$serial==serial,]
  as.character(unit$relate) %in% c("head","spouse","parent") & any(unit$relate == "child" & unit$sex==1)
}

mydata$withCM <- unlist(lapply(unique(mydata$serial),lives_with_boy ))