【发布时间】:2021-11-07 07:10:34
【问题描述】:
我有这个 df
data.frame(Name = c("AI147", "AI147", "AI147", "AI147", "AI147",
"AI20", "AI20", "AI87", "AI88", "AI88", "AI88", "AI65", "AI65"),
Presence1 = c("both_type1", "soil", "soil", "water", "both_type2",
"soil", "water", "both_type2", "soil", "soil", "soil", "water",
"water"))
我想根据每个名称的数据创建一个条件列(最终)。 (1) 如果给定名称具有多于 1 种存在类型,或仅存在“both_type1”或“both_type2”,则最终 = 两者,(2) 如果给定名称仅存在“土壤”,则最终 = 土壤, (3) 如果给定的 Name 仅存在“water”,则 Final = water,因此表格看起来像这样
data.frame(Name = c("AI147", "AI147", "AI147", "AI147", "AI147",
"AI20", "AI20", "AI87", "AI88", "AI88", "AI88", "AI65", "AI65"),
Presence1 = c("both_type1", "soil", "soil", "water", "both_type2",
"soil", "water", "both_type2", "soil", "soil", "soil", "water",
"water"),
Final = c("both", "both", "both", "both", "both", "both",
"both", "both", "soil", "soil", "soil", "water", "water"))
我已经尝试了几种我在网站上找到的方法,但没有一种方法能做到这一点。
【问题讨论】:
标签: r multiple-conditions