我有一个数据集,其中包含来自现场观察员的死鸟记录。
Death.Date Observer Species Bird.ID
1 03/08/2021 DA MF FC10682
2 15/08/2021 AG MF FC10698
3 12/01/2022 DA MF FC20957
4 09/02/2022 DA MF FC10708
我想从中生成一个具有唯一 Bird.ID/Month 数量的数据集,这样我就可以从中生成一个图表。 (“独特”是因为有些人犯错,有时会两次输入一只鸟)。
这种情况下的输出是:
Month Number of dead
08/2021 2
01/2022 1
02/2022 1
想法是使用 distinct
函数,但按月(知道值的格式为 dd/mm/yyyy)。
最佳答案
如果您的日期列是字符类型,请先使用 dmy
将格式更改为年月
group_by
和summarize
library(dplyr)
library(lubridate) # in case your Date is in character format
df %>%
mutate(Death.Date = dmy(Death.Date)) %>% # you may not need this line
mutate(Month = format(as.Date(Death.Date), "%m/%Y")) %>%
group_by(Month) %>%
summarise(`Number of dead`=n())
Month `Number of dead`
<chr> <int>
1 01/2022 1
2 02/2022 1
3 08/2021 2
https://stackoverflow.com/questions/71406135/
相关文章:
linux - "No Such Process"消耗GPU显存
Flutter - 是否应该将 firebase_options.dart 添加到 gitignor
reactjs - 如何将主页的页眉和页脚与 react 中的管理页面中的显示分开
c - 幂函数给出的答案与 C 中的 math.pow 函数不同
python - 从现有数据框中列的子字符串创建新的 Pyspark 数据框
azure-functions - VS2022 无法创建 Azure Functions 项目
spring-boot - 用 vaadin hilla 代替 flow 有什么好处