排除特定行 R
excluding Specific rows R
我想过滤掉部分数据集:
这是我的数据集:
Year Observer Quadrat ScientificName CommonName PercentageCover X_local_m Y_local_m GroundFloraRichness
1 2020 Expert 90 Glechoma hederacea Ground ivy 3.0 100 600 24
2 2020 Expert 90 Mercurialis perennis Dog's mercury 10.0 100 600 24
3 2020 Expert 90 Poa trivialis Rough meadow grass 45.0 100 600 24
4 2020 Expert 90 Pteridium aquilinum Bracken 1.0 100 600 24
5 2020 Expert 90 Rubus fruticosus Bramble 2.0 100 600 24
6 2020 Expert 90 Urtica dioica Stinging nettle 5.0 100 600 24
7 2020 Expert 91 Brachypodium sylvaticum False brome 1.0 120 600 16
8 2020 Expert 91 Galium aparine Cleavers / Goosegrass 90.0 120 600 16
9 2020 Expert 91 Glechoma hederacea Ground ivy 1.0 120 600 16
10 2020 Expert 91 Hyacinthoides non-scripta Bluebell 1.0 120 600 16
11 2020 Expert 91 Poa trivialis Rough meadow grass 60.0 120 600 16
12 2020 Expert 91 Rubus fruticosus Bramble 2.0 120 600 16
13 2020 Expert 91 Urtica dioica Stinging nettle 80.0 120 600 16
14 2021 Expert 1 Acer pseudoplatanus Sycamore 2.0 0 0 22
15 2021 Expert 1 Circaea lutetiana Enchanter's nightshade 0.5 0 0 22
16 2021 Expert 1 Deschampsia cespitosa Tufted hair grass 3.0 0 0 22
17 2021 Expert 1 Fraxinus excelsior Ash 1.0 0 0 22
18 2021 Expert 1 Geum urbanum Wood avens 0.5 0 0 22
19 2021 Expert 1 Hyacinthoides non-scripta Bluebell 20.0 0 0 22
20 2021 Expert 1 Mercurialis perennis Dog's mercury 30.0 0 0 22
21 2021 Expert 1 Pteridium aquilinum Bracken 1.0 0 0 22
22 2021 Expert 1 Rubus fruticosus Bramble 15.0 0 0 22
23 2021 Expert 2 Acer pseudoplatanus Sycamore 2.0 20 0 49
24 2021 Expert 2 Brachypodium sylvaticum False brome 1.0 20 0 49
25 2021 Expert 2 Circaea lutetiana Enchanter's nightshade 0.5 20 0 49
26 2021 Expert 2 Fraxinus excelsior Ash 15.0 20 0 49
27 2021 Expert 2 Geum urbanum Wood avens 1.0 20 0 49
df <- structure(list(Year = c(2020L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L), Observer = c("Expert", "Expert", "Expert",
"Expert", "Expert", "Expert", "Expert", "Expert", "Expert", "Expert",
"Expert", "Expert", "Expert", "Expert", "Expert", "Expert", "Expert",
"Expert", "Expert", "Expert", "Expert", "Expert", "Expert", "Expert",
"Expert", "Expert", "Expert"), Quadrat = c(90L, 90L, 90L, 90L,
90L, 90L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), ScientificName = c("Glechoma hederacea",
"Mercurialis perennis", "Poa trivialis", "Pteridium aquilinum",
"Rubus fruticosus", "Urtica dioica", "Brachypodium sylvaticum",
"Galium aparine", "Glechoma hederacea", "Hyacinthoides non-scripta",
"Poa trivialis", "Rubus fruticosus", "Urtica dioica", "Acer pseudoplatanus",
"Circaea lutetiana", "Deschampsia cespitosa", "Fraxinus excelsior",
"Geum urbanum", "Hyacinthoides non-scripta", "Mercurialis perennis",
"Pteridium aquilinum", "Rubus fruticosus", "Acer pseudoplatanus",
"Brachypodium sylvaticum", "Circaea lutetiana", "Fraxinus excelsior",
"Geum urbanum"), CommonName = c("Ground ivy", "Dog's mercury",
"Rough meadow grass", "Bracken", "Bramble", "Stinging nettle",
"False brome", "Cleavers / Goosegrass", "Ground ivy", "Bluebell",
"Rough meadow grass", "Bramble", "Stinging nettle", "Sycamore",
"Enchanter's nightshade", "Tufted hair grass", "Ash", "Wood avens",
"Bluebell", "Dog's mercury", "Bracken", "Bramble", "Sycamore",
"False brome", "Enchanter's nightshade", "Ash", "Wood avens"),
PercentageCover = c(3, 10, 45, 1, 2, 5, 1, 90, 1, 1, 60,
2, 80, 2, 0.5, 3, 1, 0.5, 20, 30, 1, 15, 2, 1, 0.5, 15, 1
), X_local_m = c(100L, 100L, 100L, 100L, 100L, 100L, 120L,
120L, 120L, 120L, 120L, 120L, 120L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 20L, 20L, 20L, 20L, 20L), Y_local_m = c(600L,
600L, 600L, 600L, 600L, 600L, 600L, 600L, 600L, 600L, 600L,
600L, 600L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), GroundFloraRichness = c(24L, 24L, 24L, 24L, 24L,
24L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 49L, 49L, 49L, 49L, 49L)), class = "data.frame", row.names = c(NA,
-27L))
我希望排除每个 Year
存在 Ash
的所有 Quadrat
到目前为止我已经尝试过:
我尝试从这里 调整解决方案,使用 (CommonName != "Ash")
作为过滤器,但这只排除了灰烬本身而不是当年的整个样方,非常感谢您的帮助。
假设我正确理解了您的需求,这应该会有所帮助:
library(dplyr)
library(tidyr)
df %>%
group_by(Year, Quadrat) %>%
mutate(helper = ifelse(CommonName %in% "Ash", TRUE, NA_character_)) %>%
fill(helper, .direction = "updown") %>%
filter(is.na(helper)) %>%
select(-helper)
Year Observer Quadrat ScientificName CommonName PercentageCover X_local_m Y_local_m GroundFloraRichness
<int> <chr> <int> <chr> <chr> <dbl> <int> <int> <int>
1 2020 Expert 90 Glechoma hederacea Ground ivy 3 100 600 24
2 2020 Expert 90 Mercurialis perennis Dog's mercury 10 100 600 24
3 2020 Expert 90 Poa trivialis Rough meadow grass 45 100 600 24
4 2020 Expert 90 Pteridium aquilinum Bracken 1 100 600 24
5 2020 Expert 90 Rubus fruticosus Bramble 2 100 600 24
6 2020 Expert 90 Urtica dioica Stinging nettle 5 100 600 24
7 2020 Expert 91 Brachypodium sylvaticum False brome 1 120 600 16
8 2020 Expert 91 Galium aparine Cleavers / Goosegrass 90 120 600 16
9 2020 Expert 91 Glechoma hederacea Ground ivy 1 120 600 16
10 2020 Expert 91 Hyacinthoides non-scripta Bluebell 1 120 600 16
11 2020 Expert 91 Poa trivialis Rough meadow grass 60 120 600 16
12 2020 Expert 91 Rubus fruticosus Bramble 2 120 600 16
13 2020 Expert 91 Urtica dioica Stinging nettle 80 120 600 16
我想过滤掉部分数据集:
这是我的数据集:
Year Observer Quadrat ScientificName CommonName PercentageCover X_local_m Y_local_m GroundFloraRichness
1 2020 Expert 90 Glechoma hederacea Ground ivy 3.0 100 600 24
2 2020 Expert 90 Mercurialis perennis Dog's mercury 10.0 100 600 24
3 2020 Expert 90 Poa trivialis Rough meadow grass 45.0 100 600 24
4 2020 Expert 90 Pteridium aquilinum Bracken 1.0 100 600 24
5 2020 Expert 90 Rubus fruticosus Bramble 2.0 100 600 24
6 2020 Expert 90 Urtica dioica Stinging nettle 5.0 100 600 24
7 2020 Expert 91 Brachypodium sylvaticum False brome 1.0 120 600 16
8 2020 Expert 91 Galium aparine Cleavers / Goosegrass 90.0 120 600 16
9 2020 Expert 91 Glechoma hederacea Ground ivy 1.0 120 600 16
10 2020 Expert 91 Hyacinthoides non-scripta Bluebell 1.0 120 600 16
11 2020 Expert 91 Poa trivialis Rough meadow grass 60.0 120 600 16
12 2020 Expert 91 Rubus fruticosus Bramble 2.0 120 600 16
13 2020 Expert 91 Urtica dioica Stinging nettle 80.0 120 600 16
14 2021 Expert 1 Acer pseudoplatanus Sycamore 2.0 0 0 22
15 2021 Expert 1 Circaea lutetiana Enchanter's nightshade 0.5 0 0 22
16 2021 Expert 1 Deschampsia cespitosa Tufted hair grass 3.0 0 0 22
17 2021 Expert 1 Fraxinus excelsior Ash 1.0 0 0 22
18 2021 Expert 1 Geum urbanum Wood avens 0.5 0 0 22
19 2021 Expert 1 Hyacinthoides non-scripta Bluebell 20.0 0 0 22
20 2021 Expert 1 Mercurialis perennis Dog's mercury 30.0 0 0 22
21 2021 Expert 1 Pteridium aquilinum Bracken 1.0 0 0 22
22 2021 Expert 1 Rubus fruticosus Bramble 15.0 0 0 22
23 2021 Expert 2 Acer pseudoplatanus Sycamore 2.0 20 0 49
24 2021 Expert 2 Brachypodium sylvaticum False brome 1.0 20 0 49
25 2021 Expert 2 Circaea lutetiana Enchanter's nightshade 0.5 20 0 49
26 2021 Expert 2 Fraxinus excelsior Ash 15.0 20 0 49
27 2021 Expert 2 Geum urbanum Wood avens 1.0 20 0 49
df <- structure(list(Year = c(2020L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L), Observer = c("Expert", "Expert", "Expert",
"Expert", "Expert", "Expert", "Expert", "Expert", "Expert", "Expert",
"Expert", "Expert", "Expert", "Expert", "Expert", "Expert", "Expert",
"Expert", "Expert", "Expert", "Expert", "Expert", "Expert", "Expert",
"Expert", "Expert", "Expert"), Quadrat = c(90L, 90L, 90L, 90L,
90L, 90L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), ScientificName = c("Glechoma hederacea",
"Mercurialis perennis", "Poa trivialis", "Pteridium aquilinum",
"Rubus fruticosus", "Urtica dioica", "Brachypodium sylvaticum",
"Galium aparine", "Glechoma hederacea", "Hyacinthoides non-scripta",
"Poa trivialis", "Rubus fruticosus", "Urtica dioica", "Acer pseudoplatanus",
"Circaea lutetiana", "Deschampsia cespitosa", "Fraxinus excelsior",
"Geum urbanum", "Hyacinthoides non-scripta", "Mercurialis perennis",
"Pteridium aquilinum", "Rubus fruticosus", "Acer pseudoplatanus",
"Brachypodium sylvaticum", "Circaea lutetiana", "Fraxinus excelsior",
"Geum urbanum"), CommonName = c("Ground ivy", "Dog's mercury",
"Rough meadow grass", "Bracken", "Bramble", "Stinging nettle",
"False brome", "Cleavers / Goosegrass", "Ground ivy", "Bluebell",
"Rough meadow grass", "Bramble", "Stinging nettle", "Sycamore",
"Enchanter's nightshade", "Tufted hair grass", "Ash", "Wood avens",
"Bluebell", "Dog's mercury", "Bracken", "Bramble", "Sycamore",
"False brome", "Enchanter's nightshade", "Ash", "Wood avens"),
PercentageCover = c(3, 10, 45, 1, 2, 5, 1, 90, 1, 1, 60,
2, 80, 2, 0.5, 3, 1, 0.5, 20, 30, 1, 15, 2, 1, 0.5, 15, 1
), X_local_m = c(100L, 100L, 100L, 100L, 100L, 100L, 120L,
120L, 120L, 120L, 120L, 120L, 120L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 20L, 20L, 20L, 20L, 20L), Y_local_m = c(600L,
600L, 600L, 600L, 600L, 600L, 600L, 600L, 600L, 600L, 600L,
600L, 600L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), GroundFloraRichness = c(24L, 24L, 24L, 24L, 24L,
24L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 49L, 49L, 49L, 49L, 49L)), class = "data.frame", row.names = c(NA,
-27L))
我希望排除每个 Year
Ash
的所有 Quadrat
到目前为止我已经尝试过:
我尝试从这里 (CommonName != "Ash")
作为过滤器,但这只排除了灰烬本身而不是当年的整个样方,非常感谢您的帮助。
假设我正确理解了您的需求,这应该会有所帮助:
library(dplyr)
library(tidyr)
df %>%
group_by(Year, Quadrat) %>%
mutate(helper = ifelse(CommonName %in% "Ash", TRUE, NA_character_)) %>%
fill(helper, .direction = "updown") %>%
filter(is.na(helper)) %>%
select(-helper)
Year Observer Quadrat ScientificName CommonName PercentageCover X_local_m Y_local_m GroundFloraRichness
<int> <chr> <int> <chr> <chr> <dbl> <int> <int> <int>
1 2020 Expert 90 Glechoma hederacea Ground ivy 3 100 600 24
2 2020 Expert 90 Mercurialis perennis Dog's mercury 10 100 600 24
3 2020 Expert 90 Poa trivialis Rough meadow grass 45 100 600 24
4 2020 Expert 90 Pteridium aquilinum Bracken 1 100 600 24
5 2020 Expert 90 Rubus fruticosus Bramble 2 100 600 24
6 2020 Expert 90 Urtica dioica Stinging nettle 5 100 600 24
7 2020 Expert 91 Brachypodium sylvaticum False brome 1 120 600 16
8 2020 Expert 91 Galium aparine Cleavers / Goosegrass 90 120 600 16
9 2020 Expert 91 Glechoma hederacea Ground ivy 1 120 600 16
10 2020 Expert 91 Hyacinthoides non-scripta Bluebell 1 120 600 16
11 2020 Expert 91 Poa trivialis Rough meadow grass 60 120 600 16
12 2020 Expert 91 Rubus fruticosus Bramble 2 120 600 16
13 2020 Expert 91 Urtica dioica Stinging nettle 80 120 600 16