輸出表的順序不正確,01/01之后是01/02而不是03/01,如何調整?
df1<- structure(
list(date2= c("01-01-2021","01-01-2021","03-01-2021","03-01-2021","01-02-2021","01-02-2021"),
Category= c("ABC","CDE","ABC","CDE","ABC","CDE"),
coef= c(5,4,0,2,4,5)),
class = "data.frame", row.names = c(NA, -6L))
x<-df1 %>%
group_by(date2) %>%
summarize(across("coef", sum),.groups = 'drop')
> x
# A tibble: 3 x 2
date2 coef
<chr> <dbl>
1 01-01-2021 9
2 01-02-2021 9
3 03-01-2021 2
預期輸出表
date2 coef
<chr> <dbl>
1 01-01-2021 9
2 03-01-2021 2
3 01-02-2021 9
如果我有以下代碼:
df1<- structure(
list(date2= c("01-01-2022","01-01-2022","03-01-2021","03-01-2021","01-02-2021","01-02-2021"),
Category= c("ABC","CDE","ABC","CDE","ABC","CDE"),
coef= c(5,4,0,2,4,5)),
class = "data.frame", row.names = c(NA, -6L))
x<-df1 %>%
group_by(date2) %>%
summarize(across("coef", sum),.groups = 'drop')%>%
arrange(date2 = as.Date(date2, format = "%d-%m-%y"))
> x
# A tibble: 3 x 2
date2 coef
<chr> <dbl>
1 01-01-2022 9
2 03-01-2021 2
3 01-02-2021 9
這個順序是不正確的,因為它01-01-2022必須是最后而不是第一個。
uj5u.com熱心網友回復:
您可以將日期列轉換為日期格式,并對其進行排列。我還建議為您的日期列使用日期格式,以便將來進行資料處理。
堿基R
x$date2 <- as.Date(x$date2, format = "%d-%m-%Y")
x[order(x$date2),]
dplyr
x %>%
arrange(date2 = as.Date(date2, format = "%d-%m-%Y")
# A tibble: 3 x 2
date2 coef
<chr> <dbl>
1 01-01-2021 9
2 03-01-2021 2
3 01-02-2021 9
uj5u.com熱心網友回復:
您需要先轉換為日期,您似乎認為是帝國的。
aggregate(coef ~ date2, transform(df1, date2=as.Date(date2, format='%m-%d-%Y')), sum)
# date2 coef
# 1 2021-01-01 9
# 2 2021-01-02 9
# 3 2021-03-01 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/411314.html
標籤:
