Home > Software engineering >  Creating an object based on specific value inside column
Creating an object based on specific value inside column

Time:04-21

So I'm pretty sure my question is very simple, but I haven't found anything clear that has helped me.

Basically, I have a dataset with information about bird densities based on observational data. Each bird is labeled with a four letter acronym denoting the species, under the column "Group."

Here's a simplified example of what it looks like -

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

How can I create an object for all rows containing the information associated only with ARTE and RAZO?

CodePudding user response:

You can do this using the subset() function. For example:

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

CodePudding user response:

We may use filter:

library(dplyr)

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

or

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

data:

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))
  • Related