R 从一个数据帧到另一个数据帧的条件随机匹配
我有两个数据帧。一个数据框(R 从一个数据帧到另一个数据帧的条件随机匹配,r,merge,match,sample,R,Merge,Match,Sample,我有两个数据帧。一个数据框(Partners.Missing)包含195个已结为伴侣的人(已婚、事实上的,等等),我需要使用第二个数据框(NAsOnly)中的随机选择来构建伴侣 合作伙伴。缺少的数据帧信息是: str(Partners.Missing) 'data.frame': 195 obs. of 8 variables: $ V1 : Factor w/ 2 levels "Female","Male": 1 1 1 2 1 1 1 2 2 2 ... $
Partners.Missing
)包含195个已结为伴侣的人(已婚、事实上的,等等),我需要使用第二个数据框(NAsOnly
)中的随机选择来构建伴侣
合作伙伴。缺少的
数据帧信息是:
str(Partners.Missing)
'data.frame': 195 obs. of 8 variables:
$ V1 : Factor w/ 2 levels "Female","Male": 1 1 1 2 1 1 1 2 2 2 ...
$ V2 : Factor w/ 9 levels "15 - 17 Years",..: 4 4 7 7 4 4 7 3 7 4 ...
$ V3 : Factor w/ 1 level "Partnered": 1 1 1 1 1 1 1 1 1 1 ...
$ V4 : Factor w/ 7 levels "Eight or More Usual Residents",..: 1 1 5 2 1 1 1 1 2 5 ...
$ V5 : Factor w/ 8 levels "1-9 Hours Worked",..: 8 4 8 6 7 8 7 5 4 6 ...
$ SEX : chr "Male" "Male" "Male" "Female" ...
$ Ageband : num 4 4 7 7 4 4 7 3 7 4 ...
$ Inhabitants: num 8 8 6 5 8 8 8 8 5 6 ...
因为V2是一个因素,所以我创建了Ageband
变量,该变量是V2
的重新编码,因此最小年龄组(15-17岁)为1,次大年龄组为2,以此类推。居民
是V4
的重新编码,再次构造一个数字变量<代码>性别是二进制的“男性”/“女性”
第二个数据帧(NAsOnly
)上的信息为:
我可以创建新变量,以便Ageband
和NAsOnly
中的incident
是相同的结构,用于匹配。但我被困在如何匹配上了。对于Partners.Missing
中的每一行,我想做的是使用以下标准从nasoly
中随机抽取一个观察值:
- 相反的
(因此性别
将与伴侣中的“女性”。缺失的
)NAsOnly中的“男性”匹配
- “女性”伴侣(无论其来源于哪个数据框)与“男性”伴侣处于同一年龄段,或比“男性”伴侣年轻一岁
是精确匹配的,因此5人家庭中的“女性”只能与5人家庭中的“男性”(年龄段正确)匹配居民的数量
中的nasoly
只能是“合作关系”(“非合作关系”和“不包括在其他地方”也是该数据框中的有效变量条目)*关系
Partners.Missing中每次观察一次,这样他们的伴侣就不会再失踪了
我也不能使用第一个或最后一个匹配,因为根据我的标准,NAsOnly
中可能有许多行匹配-它必须是随机抽取的,否则每次都会从NAsOnly
中抽取相同的观察值。基本上,类似于从nasoly
进行替换的随机抽样。无论采样的观测值是否用于构建第三个匹配数据帧,或者采样的观测值是否作为附加列添加到Partners.Missing
,都无关紧要
*它有四个级别,因为原始较大的数据帧有总计行,所以第四个(未使用的)级别是“总计”
更新:
我曾尝试编写一个for next循环来实现这一点,但它并没有按预期工作。代码是:
for(i in 1:1) {
row <- Partners.Missing[i,]
if(row$V1=="Female")
matched <- data.frame(row$SEX[i]==Partnered.Censored$SEX &
row$Inhabitants[i]==Partnered.Censored$Inhabitants &
(row$Ageband[i]==Partnered.Censored$Ageband | row$Ageband[i]==Partnered.Censored$Ageband+1)
)
else
matched <- data.frame(row$SEX[i]==Partnered.Censored$SEX &
row$Inhabitants[i]==Partnered.Censored$Inhabitants &
(row$Ageband[i]==Partnered.Censored$Ageband | row$Ageband[i]==Partnered.Censored$Ageband-1)
)
}
for(1:1中的i){
row在过去的几天里,这一直是我最想做的事情,我似乎已经用下面的代码解决了这个问题。我把这个问题留着回答,以防其他人需要这样做
for(i in 1:nrow(Partners.Missing)) {
row <- Partners.Missing[i,]
result <- merge(row, Partnered.Censored, by=c("SEX","Inhabitants"),suffixes=c(".r",".c"))
if (row$V1=="Female") {
result<- subset(result, Ageband.r==Ageband.c | Ageband.r==Ageband.c-1)
}
if (row$V1=="Male") {
result<- subset(result, Ageband.r==Ageband.c | Ageband.r==Ageband.c+1)
}
j <- sample(1:nrow(result),1)
if(i == 1) {
Matched.Partners <- result[j,]
}
if (i > 1) {
Matched.Partners <- rbind(Matched.Partners,result[j,])
}
}
for(1中的i:nrow(Partners.Missing)){
row在过去的几天里,这一直是我最想做的事情,我似乎已经用下面的代码解决了这个问题。我把这个问题留着回答,以防其他人需要这样做
for(i in 1:nrow(Partners.Missing)) {
row <- Partners.Missing[i,]
result <- merge(row, Partnered.Censored, by=c("SEX","Inhabitants"),suffixes=c(".r",".c"))
if (row$V1=="Female") {
result<- subset(result, Ageband.r==Ageband.c | Ageband.r==Ageband.c-1)
}
if (row$V1=="Male") {
result<- subset(result, Ageband.r==Ageband.c | Ageband.r==Ageband.c+1)
}
j <- sample(1:nrow(result),1)
if(i == 1) {
Matched.Partners <- result[j,]
}
if (i > 1) {
Matched.Partners <- rbind(Matched.Partners,result[j,])
}
}
for(1中的i:nrow(Partners.Missing)){
一行