I have managed to manipulate part of my data set to exclude all percentage cover values of 0, but now am looking at excluding certain quadrats from certain years that don't have ash present in them. I was wondering if there was a specific way of specifying which quadrats I wanted removed from which year as opposed to deleting the quadrats from all the years, which is not what I want to do.
CodePudding user response:
Because your data is not in a text form, here is a very simple mock data that represents the case adequately:
dat <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L),
class = c("B", "A", "B", "B", "C", "B", "B", "C", "C")),
class = "data.frame", row.names = c(NA, -9L))
dat
# id class
# 1 1 B
# 2 1 A
# 3 1 B
# 4 2 B
# 5 2 C
# 6 2 B
# 7 3 B
# 8 3 C
# 9 3 C
Suppose I want to exclude any id
that does not have C
value in its class
. So, the rows 1,2,3 should be excluded. One of the simplest way is to use dplyr
package:
library(dplyr)
dat |> group_by(id) |> filter("C" %in% class)
#The result:
# A tibble: 6 × 2
# Groups: id [2]
# id class
# <int> <chr>
#1 2 B
#2 2 C
#3 2 B
#4 3 B
#5 3 C
#6 3 C
Note that the logic is that, because I want to exclude the group that does not have C
, it means I want to keep the group that has C
.
Hence %in%
is used. It is a function that checks if a particular pattern matches a table. The pattern in this case is C
and the table is the values in class
column. I want to apply this function to each group of id, so group_by(id)
is used.
Therefore, these steps can be applied to your data in this way:
yourdf |> group_by(Quadrat) |> filter("Ash" %in% CommonName)
A base R option
In case you want to do the steps above in the base R, here is an option:
dat |>
by(dat$id,function(x) x["C" %in% x$class,])|>
do.call(rbind, args = _)
# id class
# 2.4 2 B
# 2.5 2 C
# 2.6 2 B
# 3.7 3 B
# 3.8 3 C
# 3.9 3 C