具有多个条件和 4 个结果的 If else 语句
If else statement with multiple conditions and 4 outcomes
我正在尝试根据共性对物种进行分类。共有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 语句还很陌生,如有任何帮助,我们将不胜感激。
谢谢!
我使用你的代码得到“偶尔”。
您的 if 语句查看逻辑向量,但为所有行返回一个值,例如:
df[,2]
是整列:0.50 0.11 0.23 0.06 0.36 0.19
df[,2]>mean(relabund)
returns 的逻辑向量:
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")))))
我们可以使用 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))
我正在尝试根据共性对物种进行分类。共有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 语句还很陌生,如有任何帮助,我们将不胜感激。
谢谢!
我使用你的代码得到“偶尔”。
您的 if 语句查看逻辑向量,但为所有行返回一个值,例如:
df[,2]
是整列:0.50 0.11 0.23 0.06 0.36 0.19
df[,2]>mean(relabund)
returns 的逻辑向量:
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")))))
我们可以使用 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))