两种条件过滤和去除零值
Two condition filtering and removing Zero Values
我在种植辣椒的实验中有一个简单的 dataframe 计数。我想删除 treatment's
[(Control and Covered) Fruit_total
都为零的观察结果。我试过 filter
但我一次只能处理一个变量。有什么建议吗?
]2
您可以通过对 location.id
进行分组并过滤 Fruit_total
:
的总和来完成此操作
library(tidyverse)
df %>%
group_by(location.ID) %>%
filter(sum(Fruit_total) != 0)
产量:
# A tibble: 22 x 5
# Groups: location.ID [11]
location.ID Year plant treatment Fruit_total
<dbl> <dbl> <chr> <chr> <dbl>
1 7 2019 Anaheim.Peppers.Count Control 23
2 9 2019 Anaheim.Peppers.Count Control 3
3 15 2019 Anaheim.Peppers.Count Control 0
4 23 2019 Anaheim.Peppers.Count Control 1
5 38 2019 Anaheim.Peppers.Count Control 8
6 41 2019 Anaheim.Peppers.Count Control 1
7 42 2019 Anaheim.Peppers.Count Control 12
8 43 2019 Anaheim.Peppers.Count Control 7
9 45 2019 Anaheim.Peppers.Count Control 5
10 49 2019 Anaheim.Peppers.Count Control 13
# ... with 12 more rows
我在种植辣椒的实验中有一个简单的 dataframe 计数。我想删除 treatment's
[(Control and Covered) Fruit_total
都为零的观察结果。我试过 filter
但我一次只能处理一个变量。有什么建议吗?
您可以通过对 location.id
进行分组并过滤 Fruit_total
:
library(tidyverse)
df %>%
group_by(location.ID) %>%
filter(sum(Fruit_total) != 0)
产量:
# A tibble: 22 x 5
# Groups: location.ID [11]
location.ID Year plant treatment Fruit_total
<dbl> <dbl> <chr> <chr> <dbl>
1 7 2019 Anaheim.Peppers.Count Control 23
2 9 2019 Anaheim.Peppers.Count Control 3
3 15 2019 Anaheim.Peppers.Count Control 0
4 23 2019 Anaheim.Peppers.Count Control 1
5 38 2019 Anaheim.Peppers.Count Control 8
6 41 2019 Anaheim.Peppers.Count Control 1
7 42 2019 Anaheim.Peppers.Count Control 12
8 43 2019 Anaheim.Peppers.Count Control 7
9 45 2019 Anaheim.Peppers.Count Control 5
10 49 2019 Anaheim.Peppers.Count Control 13
# ... with 12 more rows