我有以下数据集:
name1 <- c("P1", "P2", "IndA", "IndB", "IndC", "IndD", "IndE", "IndF", "IndG")
name2 <- c("P1", "P2", "IndH", "IndI", "IndJ", "IndK")
name3 <- c("P1", "P2", "IndL", "IndM", "IndN")
name <- c(name1, name2, name3)
A <- c(1, 3, 1, 2, 2, 5, 5, 1, 4, 1, 3, 3, 1, 4, 3, 1, 1, 3,2,1 )
B <- c(2, 4, 3, 4, 2, 2, 6, 2, 2, 1, 4, 3, 1, 1, 5, 2,2, 1, 2, 1 )
family = c(rep(1, length (name1)), rep(2, length (name2)), rep(3, length (name3)))
mydf <- data.frame (family, name, A, B)
以下是我要应用每个级别的家庭变量的过程:
dum.match<-rbind(expand.grid(c(mydf[1,3:4]),c(mydf[2,3:4])),
expand.grid(c(mydf[2,3:4]), c(mydf [1,3:4])))
newmydf<-cbind(mydf, correct = paste(mydf$A,mydf$B)%in%paste(dum.match$Var1,
dum.match$Var2))
所以我生成了一个函数:
err.chk <- function (x) {
dum.match<-rbind(expand.grid(c(x[1,3:4]),c(x[2,3:4])),
expand.grid(c(x[2,3:4]),c(x[1,3:4])))
newmydf<-cbind(x, correct = paste(x$A,mydf$B)%in%paste(dum.match$Var1,
dum.match$Var2))
return (newmydf)
}
现在我想为每个级别的家庭创建单独的 3 个数据集并应用上述函数并将结果组合到上述数据框中,并且附加列正确。我该怎么做 ?我试过跟随(结果很糟糕!)
require(plyr)
aaply(mydf, 1, err.chk)
编辑:
预期输出:
family name A B correct
1 1 P1 1 2 FALSE
2 1 P2 3 4 FALSE
3 1 IndA 1 3 TRUE
4 1 IndB 2 4 TRUE
5 1 IndC 2 2 FALSE
6 1 IndD 5 2 FALSE
7 1 IndE 5 6 FALSE
8 1 IndF 1 2 FALSE
9 1 IndG 4 2 TRUE
10 2 P1 1 1 FALSE
11 2 P2 3 4 FALSE
12 2 IndH 3 3 FALSE
13 2 IndI 1 1 FALSE
14 2 IndJ 4 1 TRUE
15 2 IndK 3 5 FALSE
16 3 P1 1 2 TRUE
17 3 P2 1 2 TRUE
18 3 IndL 3 1 FALSE
19 3 IndM 2 2 TRUE
20 3 IndN 1 1 TRUE
仅适用于family = 3(对于其他数据集类似)
# just data for family 3
name <- c("P1", "P2", "IndL", "IndM", "IndN")
A <- c(1, 1, 3,2,1 )
B <- c(2,2, 1, 2, 1)
mydf <- data.frame (name, A, B)
err.chk(fam3)
name A B correct
16 P1 1 2 TRUE
17 P2 1 2 TRUE
18 IndL 3 1 FALSE
19 IndM 2 2 TRUE
20 IndN 1 1 TRUE