用条件总结计数
summarize count with a condition
我有一个数据框,其中包含许多年内一堆物种的样方计数,但有时它们不是计数,而是标记为“p”,表示“现在”。我想对它们进行平均,同时在平均中将这些 p 计算为 NA,但还要跟踪每个 species/year 中 p 的数量,所以我的问题是,有没有办法使用 summarize(count) 来计算 P 的出现次数?
最小示例:
df <- data.frame(
# years
year = rep(1990:1992, each=3),
# character vector of counts and p's
count = c("p","p","2","1","5","4","7","p","4")
) %>%
# numeric column of counts and NAs where P's should be
mutate(count_numeric = as.numeric(count))
# summarize dataset
df %>%
group_by(year) %>%
summarize(number_quadrats = n(), # find total number of rows
average_count = mean(count_numeric, na.rm=T)) # find average value
但我想在摘要中添加另一行,只计算每组中 P 的数量。像这样:
df %>%
group_by(year) %>%
summarize(number_quadrats = n(), # find total number of rows
average_count = mean(count_numeric, na.rm=T),# find average value
number_p = n(count == "p"))
但这不起作用。
感谢任何建议。
谢谢!
只需更改最后一行:
df %>%
group_by(year) %>%
summarize(number_quadrats = n(), # find total number of rows
average_count = mean(count_numeric, na.rm=T),# find average value
number_p = sum(count == "p"))
通过对布尔向量求和,您实际上是在计算满足条件的次数。
像这样!
df %>%
group_by(year) %>%
summarize(N = n(), number_quadrats = sum(count == 'p'),
average_count = mean(count_numeric, na.rm=T))
year N number_quadrats average_count
<int> <int> <int> <dbl>
1 1990 3 2 2
2 1991 3 0 3.33
3 1992 3 1 5.5
我有一个数据框,其中包含许多年内一堆物种的样方计数,但有时它们不是计数,而是标记为“p”,表示“现在”。我想对它们进行平均,同时在平均中将这些 p 计算为 NA,但还要跟踪每个 species/year 中 p 的数量,所以我的问题是,有没有办法使用 summarize(count) 来计算 P 的出现次数?
最小示例:
df <- data.frame(
# years
year = rep(1990:1992, each=3),
# character vector of counts and p's
count = c("p","p","2","1","5","4","7","p","4")
) %>%
# numeric column of counts and NAs where P's should be
mutate(count_numeric = as.numeric(count))
# summarize dataset
df %>%
group_by(year) %>%
summarize(number_quadrats = n(), # find total number of rows
average_count = mean(count_numeric, na.rm=T)) # find average value
但我想在摘要中添加另一行,只计算每组中 P 的数量。像这样:
df %>%
group_by(year) %>%
summarize(number_quadrats = n(), # find total number of rows
average_count = mean(count_numeric, na.rm=T),# find average value
number_p = n(count == "p"))
但这不起作用。
感谢任何建议。
谢谢!
只需更改最后一行:
df %>%
group_by(year) %>%
summarize(number_quadrats = n(), # find total number of rows
average_count = mean(count_numeric, na.rm=T),# find average value
number_p = sum(count == "p"))
通过对布尔向量求和,您实际上是在计算满足条件的次数。
像这样!
df %>%
group_by(year) %>%
summarize(N = n(), number_quadrats = sum(count == 'p'),
average_count = mean(count_numeric, na.rm=T))
year N number_quadrats average_count
<int> <int> <int> <dbl>
1 1990 3 2 2
2 1991 3 0 3.33
3 1992 3 1 5.5