根据数据集列中的名称设置条件

让我们只保留观察次数最多的三个县:索诺玛、洛杉矶和克恩。创建一个名为 bigCounties 的条件,如果变量 TRUE 是这三个县中的任何一个,则该条件为 county。请记住,|or 逻辑运算符。

更新

bigCounties <- isTRUE( CASchools$county == "Sonoma" | CASchools$county == "Los Angeles"|CASchools$county == "Kern")
summary(bigCounties)

输出:

   Mode   FALSE 
logical       1 

如何让它运行整个数据集?

lienzhangyubo 回答:根据数据集列中的名称设置条件

我认为你应该尝试%in%,例如,

CASchools$county %in% c("Sonoma","Los Amgles","Kern")

否则,你应该尝试

CASchools$county == "Sonoma"| CASchools$county == "Los Amgles"| CASchools$county =="Kern"

或(感谢@akrun 的贡献)

Reduce(`|`,lapply(c("Sonoma","Kern"),function(x) CASchools$county == x))

如果您想使用 |

本文链接:https://www.f2er.com/244.html

大家都在问