如何使用 group_by 而不按字母顺序排序?

发布于 2025-01-17 07:48:15 字数 636 浏览 0 评论 0原文

我正在尝试可视化一些鸟类数据,但是在按月进行分组之后,产生的输出从原始数据中脱离了。它是为了在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2025-01-24 07:48:15

您还没有共享任何可用的数据,但我在读取 csv 时多次遇到这种情况,因此所有日期和数据都是字符。
按照建议,请使用 lubridate 包或基 as.Date() 将日期数据转换为“日期”格式,然后 dplyr 中的range() 将起作用,甚至 group_by

示例:创建的玩具数据

birds <- data.table(dates = c("2020-Feb-20","2020-Jan-20","2020-Dec-20","2020-Apr-20"),
           species = c('Gulls','Turns','Gulls','Sandpiper'),
           Counts = c(20,30,40,50)

str(birds) 将显示日期是字符(并且我没有保留顺序)

使用 lubridate 转换日期

birds$dates%>%lubridate::ymd() 将更改为日期数据类型

 birds$dates%>%ymd()%>%str()
 Date[1:4], format: "2020-02-20" "2020-01-20" "2020-12-20" "2020-04-20"

保存它与 birds$dates <- ymd(birds$dates) 或在您的管道中执行如下操作

,以便 dplyr 分析:

    birds%>%group_by(Months= ymd(dates))%>%
  summarise(N=n()
            ,Species_Count = sum(Counts)
            )%>%arrange(Months)

将给出

# A tibble: 4 x 3
  Months         N Species_Count
  <date>     <int>         <dbl>
1 2020-01-20     1            30
2 2020-02-20     1            20
3 2020-04-20     1            50

但是,如果您想要 Apr , Jan 而不是数字并应用as.Date() 具有格式等,日期再次变成“字符”。我建议您以这种方式保存数据,并在输出中为其他人表示 ->使用 as.Date 对其进行格式化,或者如果使用 DT 或其他数据表 ->检查输出格式选项。这样您的原始数据就会保留下来,用户就会看到他们想要的内容。
这将使其成为字符

 birds%>%group_by(Months= as.character.Date(dates))%>%
   summarise(N=n()
             ,Species_Count = sum(Counts)
   )%>%arrange(Months)

A tibble:4 x 3

Months          N Species_Count
  <chr>       <int>         <dbl>
1 2020-Apr-20     1            50
2 2020-Dec-20     1            40
3 2020-Feb-20     1            20
4 2020-Jan-20     1            30

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

birds <- data.table(dates = c("2020-Feb-20","2020-Jan-20","2020-Dec-20","2020-Apr-20"),
           species = c('Gulls','Turns','Gulls','Sandpiper'),
           Counts = c(20,30,40,50)

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-type

 birds$dates%>%ymd()%>%str()
 Date[1:4], format: "2020-02-20" "2020-01-20" "2020-12-20" "2020-04-20"

save it with birds$dates <- ymd(birds$dates) or do it in your pipeline as follows

now simply so the dplyr analysis:

    birds%>%group_by(Months= ymd(dates))%>%
  summarise(N=n()
            ,Species_Count = sum(Counts)
            )%>%arrange(Months)

will give

# A tibble: 4 x 3
  Months         N Species_Count
  <date>     <int>         <dbl>
1 2020-01-20     1            30
2 2020-02-20     1            20
3 2020-04-20     1            50

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

 birds%>%group_by(Months= as.character.Date(dates))%>%
   summarise(N=n()
             ,Species_Count = sum(Counts)
   )%>%arrange(Months)

A tibble: 4 x 3

Months          N Species_Count
  <chr>       <int>         <dbl>
1 2020-Apr-20     1            50
2 2020-Dec-20     1            40
3 2020-Feb-20     1            20
4 2020-Jan-20     1            30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文