如何使用 group_by 而不按字母顺序排序?
我正在尝试可视化一些鸟类数据,但是在按月进行分组之后,产生的输出从原始数据中脱离了。它是为了在12月,1月,2月和三月的原始作品中进行,但是在操纵它之后,它于12月,2月,1月,3月。
有什么想法我如何解决此问题或对行进行排序?
这是代码:
BirdDataTimeClean <- BirdDataTimes %>%
group_by(Date) %>%
summarise(Gulls=sum(Gulls), Terns=sum(Terns), Sandpipers=sum(Sandpipers),
Plovers=sum(Plovers), Pelicans=sum(Pelicans), Oystercatchers=sum(Oystercatchers),
Egrets=sum(Egrets), PeregrineFalcon=sum(Peregrine_Falcon), BlackPhoebe=sum(Black_Phoebe),
Raven=sum(Common_Raven))
BirdDataTimeClean2 <- BirdDataTimeClean %>%
pivot_longer(!Date, names_to = "Species", values_to = "Count")
I'm trying to visualize some bird data, however after grouping by month, the resulting output is out of order from the original data. It is in order for December, January, February, and March in the original, but after manipulating it results in December, February, January, March.
Any ideas how I can fix this or sort the rows?
This is the code:
BirdDataTimeClean <- BirdDataTimes %>%
group_by(Date) %>%
summarise(Gulls=sum(Gulls), Terns=sum(Terns), Sandpipers=sum(Sandpipers),
Plovers=sum(Plovers), Pelicans=sum(Pelicans), Oystercatchers=sum(Oystercatchers),
Egrets=sum(Egrets), PeregrineFalcon=sum(Peregrine_Falcon), BlackPhoebe=sum(Black_Phoebe),
Raven=sum(Common_Raven))
BirdDataTimeClean2 <- BirdDataTimeClean %>%
pivot_longer(!Date, names_to = "Species", values_to = "Count")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有共享任何可用的数据,但我在读取 csv 时多次遇到这种情况,因此所有日期和数据都是字符。
按照建议,请使用 lubridate 包或基 as.Date() 将日期数据转换为“日期”格式,然后 dplyr 中的range() 将起作用,甚至 group_by
示例:创建的玩具数据
str(birds)
将显示日期是字符(并且我没有保留顺序)使用 lubridate 转换日期
birds$dates%>%lubridate::ymd()
将更改为日期数据类型保存它与
birds$dates <- ymd(birds$dates)
或在您的管道中执行如下操作,以便 dplyr 分析:
将给出
但是,如果您想要 Apr , Jan 而不是数字并应用as.Date() 具有格式等,日期再次变成“字符”。我建议您以这种方式保存数据,并在输出中为其他人表示 ->使用 as.Date 对其进行格式化,或者如果使用 DT 或其他数据表 ->检查输出格式选项。这样您的原始数据就会保留下来,用户就会看到他们想要的内容。
这将使其成为字符
A tibble:4 x 3
You haven't shared any workable data but i face this many times when reading from csv and hence all dates and data are in character.
as suggested, please convert the date data to "date" format using lubridate package or base as.Date() and then arrange() in dplyr will work or even group_by
example :toy data created
str(birds)
will show date is character (and I have not kept order)using lubridate convert dates
birds$dates%>%lubridate::ymd()
will change to date data-typesave it with
birds$dates <- ymd(birds$dates)
or do it in your pipeline as followsnow simply so the dplyr analysis:
will give
However, if you want Apr , Jan instead of numbers and apply as.Date() with format etc, the dates become "character" again. I woudl suggest you keep your data that way and while representing in output for others -> format it there with as.Date or if using DT or other datatables -> check the output formatting options. That way your original data remains and users see what they want.
this will make it character
A tibble: 4 x 3