3
我有兩個data.frames的濾波data.frame
data_qual
看起來像這樣:R:比較,並根據條件
data_qual <- structure(list(NAME = structure(1:3, .Label = c("NAME1", "NAME2", "NAME3"), class = "factor"), ID = c(56L, 47L, 77L), YEAR = c(1990L, 2007L, 1899L), VALUE = structure(c(2L, 1L, 1L), .Label = c("ST", "X"), class = "factor")), .Names = c("NAME", "ID", "YEAR", "VALUE"), class = "data.frame", row.names = c(NA, -3L))
NAME ID YEAR VALUE
1 NAME1 56 1990 X
2 NAME2 47 2007 ST
3 NAME3 77 1899 ST
我想通過比較其他數據幀dat
過濾掉來自data_qual
值:
dat <- structure(list(NAME = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("NAME1","NAME2"), class = "factor"), ID = c(56L, 56L, 56L, 47L, 47L, 47L, 47L), YEAR = c(1988L, 1989L, 1991L, 2005L, 2006L, 2007L, 2008L), VALUE = c(45L, 28L, 28L, -12L, 14L, 23L, 32L)), .Names = c("NAME", "ID", "YEAR", "VALUE"), class = "data.frame", row.names = c(NA, -7L))
NAME ID YEAR VALUE
1 NAME1 56 1988 45
2 NAME1 56 1989 28
3 NAME1 56 1991 28
4 NAME2 47 2005 -12
5 NAME2 47 2006 14
6 NAME2 47 2007 23
7 NAME2 47 2008 32
我怎麼能只基於行寫入到一個新的data.frame
噸在第一濾波處理列ID
使過濾data_qual
帽子有匹配ID
與dat
?
NAME ID YEAR VALUE
1 NAME1 56 1990 X
2 NAME2 47 2007 ST
然後在那之後我正在尋找一種方式,從產生的data.frame僅行應寫出不每組有相同YEAR
(由ID
定義)
NAME ID YEAR VALUE
1 NAME1 56 1990 X
任何幫助,慷慨讚賞。
您有興趣的中間步驟還是直接最後一步? – Arun 2014-09-23 20:02:19
中間步驟很好看,這樣我可以更好地控制數據。 – kurdtc 2014-09-23 20:15:45