2015-10-20 136 views
1

我有兩個數據幀。一個數據幀(Partners.Missing)包含195個人,我需要使用第二個數據幀(NAsOnly)中的隨機選擇構建合作伙伴,這些人是合夥人(已婚,事實上等)。從一個數據幀到另一個數據幀的條件隨機匹配

Partners.Missing數據幀信息是:

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等。InhabitantsV4的重新編碼,再次構造數字變量。 Sex是二元「男」/「女」。

第二數據幀(NAsOnly)上的信息是:

str(NAsOnly) 
'data.frame': 762 obs. of 7 variables: 
    $ SEX   : Factor w/ 3 levels "Female","Male",..: 2 2 2 2 2 2 2 2 2 2 ... 
    $ AGEBAND  : Factor w/ 13 levels "0 - 4 Years",..: 3 3 3 3 3 3 3 3 3 3 ... 
    $ RELATIONSHIP: Factor w/ 4 levels "Non-partnered",..: 3 3 3 3 1 1 1 1 1 1 ... 
    $ INHABITANTS : Factor w/ 9 levels "Eight or More Usual Residents",..: 7 7 3 2 9 9 9 9 7 7 ... 
    $ HRSWORKED : Factor w/ 9 levels "1-9 Hours Worked",..: 1 8 6 3 1 2 3 6 3 4 ... 

我可以創建新的變量,以便在NAsOnlyAgebandInhabitants具有相同的結構,在匹配中使用。但我堅持如何配合。我想做的事 - 每個行Partners.Missing - 是隨機NAsOnly使用下列標準樣本的觀察:

  • 相反SEX(這樣一個「女」在Partners.MissingNAsOnly匹配到一個「男」 )
  • 中的「女性」夥伴(無論哪個數據幀的它們起源)是在相同的年齡段,或一個年輕的,比「男性」夥伴
  • Inhabitants數爲完全匹配,以使得來自5人家庭的「女性」只能與來自5人家庭的「男性」(正確的年齡段)匹配
  • NAsOnlyRELATIONSHIP只能是「合作」(「非結成夥伴的」和「不包括其他地方」,可以在該數據幀有效的變量條目)*。

所以我想要一對一的比賽,我需要比賽是一個隨機抽籤,而不是第一個可用。並且做這195次,每次觀察Partners.Missing一次,以便他們的伴侶不再失蹤。

我不能使用第一個或最後一個匹配,因爲根據我的標準,NAsOnly可能有很多行匹配 - 它必須是隨機抽取,否則每次都會抽取相同的觀察值NAsOnly。基本上,像從NAsOnly替換隨機抽樣。使用採樣觀察值構造第三個匹配數據幀還是將採樣觀察值作爲附加列添加到Partners.Missing無關緊要。

*它有四個級別與原始較大的數據幀已經總計行,所以第四個(和未使用的)電平爲「總計」。

更新: 我試圖寫一個爲下一個循環做到這一點,但預期它不工作。的代碼是:

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

此輸出單個柱成data frame稱爲matchedTRUEFALSE如在277行的單一列的輸入,表示該行的在Partnered.Censored索引是否爲匹配或沒有。一旦我把i的最大值增加到2(知道我有195行),我得到NA作爲輸出。我有剩餘存在以下問題:

  • 我希望用行(S),從Partnered.Censored匹配,而不是輸出一個布爾結果
  • 我則想從匹配的行隨機抽樣產生的新的合作伙伴
  • 然後重複爲Partners.Missing中的每一行。

我也有問題在哪裏增加最大值i,例如,到2,將覆蓋TRUE/ FALSE values with NA`的單列。

回答

0

這一直頂我的腦海,在過去幾天,我似乎使用下面的代碼已經解決了這個問題。我會留下問題並回答,以防萬一其他人需要這樣做。

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

解釋這段代碼給任何人太需要這個答案,同時也看到,如果社區有一個更好的答案, 中的每個人Partners.Missing臨時向量創建抱着那個人的信息。一對多連接是基於兩個變量的基礎上構建的 - 失蹤者的性別以及家庭中的居民人數。然後,取決於Partners.Missing中的人是女性還是男性,匹配結果僅保留給具有正確年齡段的潛在伴侶。該代碼然後查找所識別的潛在夥伴的數量,並生成1和該數字之間的隨機整數。這用於提取隨機匹配的人並將其放入輸出數據框。由於在運行此代碼之前輸出數據幀(Matched.Partners)不存在,因此第一個循環將在其第一行中創建數據幀。每隔一段時間,數據幀就已經存在,所以新的匹配被追加。

我不投了無論是我的問題還是我的答案。

相關問題