您可以通过将数据重新整形为长格式然后按行值过滤来完成此操作。
df = data.frame(Quadrat = 1:6, Date = seq.Date(as.Date("2014-01-01"), by = 1, length = 6), Species_1 = c("unk1", "bope", "bope", "stgu", "bg","bope"),
covrage = sample(1:100,6), Species_2 = c("bope", "bial", "stgu", "bg","unk1", "bg"), covrage2 = sample(1:100,6))
> df
Quadrat Date Species_1 covrage Species_2 covrage2
1 1 2014-01-01 unk1 76 bope 63
2 2 2014-01-02 bope 82 bial 33
3 3 2014-01-03 bope 41 stgu 5
4 4 2014-01-04 stgu 6 bg 45
5 5 2014-01-05 bg 65 unk1 21
6 6 2014-01-06 bope 15 bg 96
df$Species_1 = as.character(df$Species_1)
df$Species_2 = as.character(df$Species_2)
df2 = reshape(df, varying = list(c("Species_1", "Species_2"), c("covrage", "covrage2")), v.names = c("Species", "Covrage"), direction = "long")
> df2
Quadrat Date time Species Covrage id
1.1 1 2014-01-01 1 unk1 76 1
2.1 2 2014-01-02 1 bope 82 2
3.1 3 2014-01-03 1 bope 41 3
4.1 4 2014-01-04 1 stgu 6 4
5.1 5 2014-01-05 1 bg 65 5
6.1 6 2014-01-06 1 bope 15 6
1.2 1 2014-01-01 2 bope 63 1
2.2 2 2014-01-02 2 bial 33 2
3.2 3 2014-01-03 2 stgu 5 3
4.2 4 2014-01-04 2 bg 45 4
5.2 5 2014-01-05 2 unk1 21 5
6.2 6 2014-01-06 2 bg 96 6
> df2[df2$Species == "bope", colnames(df2) %in% c("Quadrat", "Covrage")]
Quadrat Covrage
2.1 2 82
3.1 3 41
6.1 6 15
1.2 1 63