我有一个数据框,其中包含多年中多个物种的象限计数,但有时它们标记为“p”表示“存在”。 我想在平均计算时将它们视为NA来计算平均值,但同时还要跟踪每个物种/年份中 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"))
但是那样行不通。
欢迎任何建议。
谢谢!