我正在嘗試撰寫一個if statement檢查一個或另一個分類變數是否在我的資料框中的列中。因此,我正在使用%in%. 當我有 1 個變數時,它作業得很好:
if("setosa" %in% iris$Species){
print("hi")
}
[1] "hi"
但是如果我有條件我不能使用它OR。
# it should return TRUE because "setosa" is within the column Species
if(("setosa" | "new") %in% iris$Species){
print("hi")
}
Error in "setosa" | "virginica" :
operations are possible only for numeric, logical or complex types
有誰知道該怎么做,或者我是否可以使用另一個函式來檢查我if statement的值是 TRUE 還是 FALSE?
提前致謝
uj5u.com熱心網友回復:
將條件放入向量中并檢查是否有任何為真(用于 OR,all用于 AND)any(c("setosa","new") %in% iris$Species)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/517968.html
上一篇:在Google表格中查找多個列并從匹配項中回傳一個串列
下一篇:根據列值顯示/隱藏按鈕
