創建數據
library(dplyr)
dtf <- read.table(text = "Name Inventory
Amy Bacon
Amy Lettuce
Amy Tomato
John Bacon
John Tomato
Katie Bacon
Katie Lettuce
Katie Tomato ", header = TRUE, stringsAsFactors = FALSE)
生成名稱和配料所需recipee所有組合
desiredrecipe <- expand.grid(Inventory = c("Bacon", "Lettuce", "Tomato"),
Name = unique(dtf$Name),
stringsAsFactors = FALSE)
numberofingredients <- length(unique(desiredrecipe$Inventory))
檢查和配料名稱的所有組合都存在於所需recipee
dtf2 <- dtf %>%
# say that it's present in the list
mutate(present = 1) %>%
full_join(desiredrecipe, by = c("Name","Inventory")) %>%
group_by(Name) %>%
mutate(BLT_Flag = ifelse(sum(present)==numberofingredients,1,0))
# replace NA values by 0
dtf2$BLT_Flag[is.na(dtf2$BLT_Flag)] <- 0
dtf2
# Name Inventory present BLT_Flag
# <chr> <chr> <dbl> <dbl>
# 1 Amy Bacon 1 1
# 2 Amy Lettuce 1 1
# 3 Amy Tomato 1 1
# 4 John Bacon 1 0
# 5 John Tomato 1 0
# 6 Katie Bacon 1 1
# 7 Katie Lettuce 1 1
# 8 Katie Tomato 1 1
# 9 John Lettuce NA 0
重現的例子將不勝感激。 http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – snoram
我錯誤地輸入了我之前的例子。我希望上面的表格更有意義。 – mrp
是否可能出現重複行,或者如果某個特定名稱出現3次,那麼BLT = 1? – snoram