如果语句带有多个条件和4个结果

3
我正在尝试基于物种的普遍性进行分类,共有4个分类:
  1. 稀有 - 频率<均值 & 相对丰度<均值
  2. 偶发 - 频率<均值 & 相对丰度>均值
  3. 常见 - 频率>均值 & 相对丰度<均值
  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"

但是这样做并不起作用,因为它会将所有物种都分类为“Rare”。我非常新手 if else 语句,所以任何帮助将不胜感激。

谢谢!

2个回答

4
我使用你的代码得到了"Occaisonal"。
你的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 通过使用&&,你正在对两个逻辑向量进行逻辑比较。由于这些向量不相同,所以始终会得到false: df[,2]>mean(relabund) && df[,3]>mean(freq)

==

c(TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) && c(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE)

==

错误

此外,df$Classification 将整列设置为相同的值,即它是在整个数据集上工作,而不是逐行进行操作。你需要对每一行执行向量操作。

使用 dplyr 来自 tidyverse 的库,你可以得到一个更易读的答案(对某些人来说!)

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")))))

1
非常感谢!我甚至没有想到这些参数会返回逻辑值。我正在尝试使用tidyverse,这样更清洁! - elisemillar

3
我们可以使用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))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接