2014-02-09 84 views
0

假設我有以下數據集。在'R'中實現代碼?

Index-----Country------Age------Time-------Response 
--------------------------------------------------- 
1------------------Germany-----------20-30----------15-20------------------1 

2------------------Germany-----------20-30----------15-20------------------NA 

3------------------Germany-----------20-30----------15-20------------------1 

4------------------Germany-----------20-30----------15-20------------------0 

5------------------France--------------20-30----------30-40------------------1 

而且我想基於以下

  1. 所列的標準查找的國家,年齡和時間都精確匹配填寫NA。即。索引1,3和4
  2. 從這些匹配的 行的響應列中隨機選擇一個值。即1,1或0
  3. 更換NA與這個新的價值

而且我想它繼續以同樣的方式進行的NA的數據集的其餘部分。

我是'R'的新手,無法弄清楚如何對其進行編碼。

+0

請提供一個可重現的例子。 –

+0

你想用數據中的所有減號做什麼? – Spacedman

回答

2

下面是使用 「data.table」 包一個做法:

DT <- data.table(mydf, key = "Country,Age,Time") 
DT[, R2 := ifelse(is.na(Response), sample(na.omit(Response), 1), 
        Response), by = key(DT)] 
DT 
# Index Country Age Time Response R2 
# 1:  5 France 20-30 30-40  1 1 
# 2:  6 France 20-30 30-40  NA 2 
# 3:  7 France 20-30 30-40  2 2 
# 4:  1 Germany 20-30 15-20  1 1 
# 5:  2 Germany 20-30 15-20  NA 1 
# 6:  3 Germany 20-30 15-20  1 1 
# 7:  4 Germany 20-30 15-20  0 0 

同樣,在基礎R,你可以嘗試ave

within(mydf, { 
    R2 <- ave(Response, Country, Age, Time, FUN = function(x) { 
    ifelse(is.na(x), sample(na.omit(x), 1), x) 
    }) 
}) 

對不起,忘分享我正在使用的示例數據:

mydf <- structure(list(Index = 1:7, Country = c("Germany", "Germany", 
"Germany", "Germany", "France", "France", "France"), Age = c("20-30", 
"20-30", "20-30", "20-30", "20-30", "20-30", "20-30"), Time = c("15-20", 
"15-20", "15-20", "15-20", "30-40", "30-40", "30-40"), Response = c(1L, 
NA, 1L, 0L, 1L, NA, 2L)), .Names = c("Index", "Country", "Age", 
"Time", "Response"), class = "data.frame", row.names = c(NA, -7L))