根据列内的特定值创建对象

Creating an object based on specific value inside column

所以我很确定我的问题很简单,但我还没有找到任何对我有帮助的明确内容。

基本上,我有一个数据集,其中包含基于观测数据的鸟类密度信息。在“组”栏下,每只鸟都标有表示该物种的四个字母首字母缩略词。

这是一个简化的示例 -

df <- 
structure(list(Group = c("ARTE", "RAZO", "LBBG", "LESP", "RAZO", 
"ARTE"), Month = c(405067L, 405067L, 405067L, 405067L, 405067L, 
405067L), Stratum = c("g200", "g100", "g100", "g100", "g300", 
"g300")), class = "data.frame", row.names = c(NA, -6L))

  Group  Month Stratum
1  ARTE 405067    g200
2  RAZO 405067    g100
3  LBBG 405067    g100
4  LESP 405067    g100
5  RAZO 405067    g300
6  ARTE 405067    g300

如何为仅包含与 ARTERAZO 关联的信息的所有行创建一个对象?

您可以使用 subset() 函数执行此操作。例如:

df1 <- subset(df, Group == "ARTE"|Group == "RAZO")

  Group  Month Stratum
1  ARTE 405067    g200
2  RAZO 405067    g100
5  RAZO 405067    g300
6  ARTE 405067    g300

我们可以使用filter:

library(dplyr)

df1 <- df %>% 
  filter(Group == "ARTE" | Group == "RAZO")

df1 <- df %>% 
  filter(Group %in% c("ARTE","RAZO"))
  Group  Month Stratum
1  ARTE 405067    g200
2  RAZO 405067    g100
3  RAZO 405067    g300
4  ARTE 405067    g300

数据:

df <- 
structure(list(Group = c("ARTE", "RAZO", "LBBG", "LESP", "RAZO", 
"ARTE"), Month = c(405067L, 405067L, 405067L, 405067L, 405067L, 
405067L), Stratum = c("g200", "g100", "g100", "g100", "g300", 
"g300")), class = "data.frame", row.names = c(NA, -6L))