Вот два решения, использующие dplyr
, второе также использует stringr
. Этот код работает с некоторыми примерами данных, аналогичными вашим.
df<-data.frame(Sp = c(rep(paste("Quercus",seq(1,10,1),sep=" "),3),
rep(paste("Acer",seq(1,10,1),sep=" "),3)),
Info1 = rnorm(60,100,20),
Info2 = rnorm(60,50,5))
Если вы заинтересованы в выборе только определенных видов, первый вариант будет более полезным.
library(dplyr)
#The species you are interested in analysing
species_of_interest<-c("Quercus 1", "Quercus 2", "Quercus 10")
#Then filter and save it in df1 object
df1<-df %>%
filter(Sp %in% species_of_interest)
Однако, если Вы заинтересованы в анализе всех видов одного конкретного рода, таких как Quercus , этот подход может быть более полезным:
#select every species containing the word "Quercus" (which can be interpreted as all
#the species of the genus Quercus).
library(dplyr)
library(stringr)
df1<-df %>%
filter(str_detect(Sp, "Quercus"))