% arrange(Year, Quarter) %>% sel-6ren">
gpt4 book ai didi

r - 如何按日期对数据框进行排序。但日期列在宿舍

转载 作者:行者123 更新时间:2023-12-05 09:30:30 24 4
gpt4 key购买 nike

enter image description here

Date <- c("Q1 2005", "Q1 2006","Q1 2007","Q1 2008","Q2 2005", "Q2 2006","Q2 2007","Q2 2008","Q3 2005", "Q3 2006","Q3 2007","Q3 2008","Q4 2005", "Q4 2006","Q4 2007","Q4 2008")

我需要按时间顺序排序。

最佳答案

另一种方法是:

library(dplyr)
libray(tidyr)
df %>%
separate(Date, c("Quarter", "Year"), remove = FALSE) %>%
arrange(Year, Quarter) %>%
select(-Quarter, -Year)
      Date
1 Q1 2005
2 Q2 2005
3 Q3 2005
4 Q4 2005
5 Q1 2006
6 Q2 2006
7 Q3 2006
8 Q4 2006
9 Q1 2007
10 Q2 2007
11 Q3 2007
12 Q4 2007
13 Q1 2008
14 Q2 2008
15 Q3 2008
16 Q4 2008

数据:

df <- data.frame(Date = c("Q1 2005", "Q1 2006","Q1 2007","Q1 2008","Q2 2005", "Q2 2006","Q2 2007","Q2 2008","Q3 2005", "Q3 2006","Q3 2007","Q3 2008","Q4 2005", "Q4 2006","Q4 2007","Q4 2008"))

关于r - 如何按日期对数据框进行排序。但日期列在宿舍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69501712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com