我想確定包含指標的組。在下面的示例中,我想確定包含county == 'other'
的districts
。如果district
中有任何county == 'other'
,那麼我希望指示變量爲1
,否則0
,對於該區域中的每一行。下面是split
,lapply
和any
這樣做的幾個嘗試,但是它們都不起作用。也許我可以提取所有的行爲county == 'other'
,爲該子集定義一個指示符,然後將該子集合並回原始數據集,但我一直認爲必須有一個更簡單的方法。感謝您的任何建議。標識包含指標的組
df.1 <- read.table(text = '
state district county apples
AA EC AB 100
AA EC BC 10
AA EC DC 150
AA C FG 200
AA C other 20
AA C HC 250
AA WC RT 300
AA WC TT 30
AA WC other 350
', header=TRUE, stringsAsFactors = FALSE)
desired.result <- read.table(text = '
state district county apples indicator
AA EC AB 100 0
AA EC BC 10 0
AA EC DC 150 0
AA C FG 200 1
AA C other 20 1
AA C HC 250 1
AA WC RT 300 1
AA WC TT 30 1
AA WC other 350 1
', header=TRUE, stringsAsFactors = FALSE)
# various attempts that do not work
with(df.1, lapply(split(county, district), function(x) {any(x)=='county' <- 1}))
with(df.1, lapply(split(county, district), function(x) {ifelse(any(x)=='other', 1, 0)}))
with(df.1, lapply(split(county, district), function(x) {any(x)=='other'}))
with(df.1, lapply(split(df.1 , district), function(x) {any(x$county)=='other'}))
with(df.1, lapply(split(county, district), function(x) {x=='other'}))
編輯
這裏是集/合併的方法,我上面提到的:我更喜歡使用基地R.