我正在嘗試根據共性對物種進行分類。有4個分類:
- 稀有 - 頻率<平均值和相對豐度<平均值
- 偶爾 - 頻率<平均值和相對豐度>平均值
- 常見 - 頻率>平均和相對豐度<平均
- 顯性 - 頻率>平均值和相對豐度>平均值
我正在嘗試創建一個 if else 陳述句,以將具有這些分類的列添加到我的資料框中,如下所示
species <- c("a", "b", "c", "d", "e", "f")
relabund <- c(.5, .11, .23, .06, .36, .19) #relative abundance
freq <- c(6, 3, 20, 2, 11, 4) #number of sites species occurs at
df = data.frame(species, relabund, freq)
我試過這樣的事情:
if (df[,2]>mean(relabund) && df[,3]>mean(freq)) {
df$Classification = "Dominant"
} else if (df[,2]<mean(relabund) && df[,3]<mean(freq)) {
df$Classification = "Rare"
} else if (df[,2]<mean(relabund) && df[,3]>mean(freq)) {
df$Classification = "Common"
} else
df$Classification = "Occasional"
但這不起作用,因為它將所有物種都歸類為“稀有”。我對 if else 陳述句非常陌生,因此將不勝感激。
謝謝!
uj5u.com熱心網友回復:
我使用您的代碼得到“偶爾”。
您的 if 陳述句正在查看邏輯向量,但為所有行回傳一個值,例如:
df[,2]是整列:0.50 0.11 0.23 0.06 0.36 0.19
df[,2]>mean(relabund)回傳一個邏輯向量:
TRUE FALSE FALSE FALSE TRUE FALSE
通過使用&&,您正在對兩個邏輯向量進行邏輯比較。由于這些向量不一樣,你總是會得到錯誤的:
df[,2]>mean(relabund) && df[,3]>mean(freq)
==
c(TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) && c(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE)
==
FALSE
此外,df$Classification將列設定為相同的值,即它在整個資料集上作業,而不是逐行處理。你需要做的是對每一行執行向量操作。
使用 dplyr 您可以獲得更容易閱讀的答案(對于某些人來說!)
library(tidyverse)
species <- c("a", "b", "c", "d", "e", "f")
relabund <- c(.5, .11, .23, .06, .36, .19) #relative abundance
freq <- c(6, 3, 20, 2, 11, 4) #number of sites species occurs at
df = data.frame(species, relabund, freq)
df %>%
mutate(classify =
ifelse(freq < mean(freq) & relabund < mean(relabund),
"Rare",
ifelse(freq < mean(freq) & relabund > mean(relabund),
"Occaisonal",
ifelse(freq > mean(freq) & relabund < mean(relabund),
"Common",
ifelse(freq > mean(freq) & relabund > mean(relabund),
"Dominant",
"ERROR")))))
uj5u.com熱心網友回復:
我們可以使用case_when,其中if左側~和右側是您要分配給該條件的值。
library(tidyverse)
df %>%
mutate(classify = case_when(freq < mean(freq) & relabund < mean(relabund) ~ "Rare",
freq < mean(freq) & relabund > mean(relabund) ~ "Occaisonal",
freq > mean(freq) & relabund < mean(relabund) ~ "Common",
freq > mean(freq) & relabund > mean(relabund) ~ "Dominant",
TRUE ~ "ERROR"))
輸出
species relabund freq classify
1 a 0.50 6 Occaisonal
2 b 0.11 3 Rare
3 c 0.23 20 Common
4 d 0.06 2 Rare
5 e 0.36 11 Dominant
6 f 0.19 4 Rare
資料
df <- structure(list(species = c("a", "b", "c", "d", "e", "f"), relabund = c(0.5,
0.11, 0.23, 0.06, 0.36, 0.19), freq = c(6, 3, 20, 2, 11, 4)), class = "data.frame", row.names = c(NA,
-6L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464030.html
