gpt4 book ai didi

r - 如何将 difftime 的值从小时数转换为天数?

转载 作者:行者123 更新时间:2023-12-04 02:27:30 26 4
gpt4 key购买 nike

我有看起来像的数据框。

structure(list(format_date = c("Feb-21 (W5)", "Mar-21 (W1)", 
"Mar-21 (W2)"), Total_count = c(53L, 1079L, 1288L), Count_Diff_1 = c(29L,
508L, 519L), Count_Diff_2 = c(22L, 505L, 624L), Count_Diff_3 = c(2L,
66L, 145L), Average_Diff_1 = structure(c(10.3464846743295, 6.03846784776903,
2.75151412973667), class = "difftime", units = "hours"), Average_Diff_2 = structure(c(6.46958333333333,
7.78101430143014, 2.58788906695157), class = "difftime", units = "hours"),
Average_Diff_3 = structure(c(162.45419129418, 133.452629846369,
54.3061989570153), class = "difftime", units = "hours"),
Per_Diff_1 = c(0.547169811320755, 0.470806302131603, 0.402950310559006
), Per_Diff_2 = c(0.415094339622642, 0.468025949953661, 0.484472049689441
), Per_Diff_3 = c(0.0377358490566038, 0.0611677479147359,
0.112577639751553)), row.names = c(NA, -3L), groups = structure(list(
format_date = c("Feb-21 (W5)", "Mar-21 (W1)", "Mar-21 (W2)"
), .rows = structure(list(1L, 2L, 3L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 3L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))

如果在 Average_1Average_2Average_3 列中超过 24 小时,我想以天为单位转换值并隐藏 Per_Diff_1Per_Diff_2Per_Diff_3,百分比保留 2 个小数点,带 % 符号。

另外,如果我能设法以降序排列 format_date 就好了。现在它是 Feb-21 (W5)Mar-21 (W1)Mar-21 (W2) 它应该是Mar-21 (W2)Mar-21 (W1)Feb-21 (W5)

必需的 df<-

format_date   Total_count Count_Diff_1 Count_Diff_2 Count_Diff_3 Average_1    Average_2    Average_3 Per_Diff_1 Per_Diff_2 Per_Diff_3
Feb-21 (W5) 53 29 22 2 10.34 hours 6.46 hours 6.7 Days 54.71% 41.50% 3.77%

最佳答案

我们可以在 starts_with 'Average' 的列上使用 across 来转换值,如果它大于 24 然后除以 24,附加 'Days' 作为后缀,或者通过附加“小时”作为后缀返回。类似地,starts_with 'Per' 的列可以使用来自 scaleslabel_percent 格式化为百分比。然后,在转换为 yearmon 类(来自 zoo::as.yearmon)

library(dplyr)
library(stringr)
library(scales)
df1 %>%
ungroup %>%
mutate(across(starts_with('Average'),
~ case_when(as.numeric(.) > 24 ~ str_c(round(as.numeric(.)/24, 2),
' Days'),
TRUE ~ str_c(as.character(round(., 2)), ' hours'))),
across(starts_with('Per'), label_percent())) %>%
arrange(desc(zoo::as.yearmon(str_remove(format_date, '\\s+.*'), '%b-%y')),
desc(readr::parse_number(str_remove_all(format_date, '.*\\(|\\)'))))

-输出

# A tibble: 3 x 11
# format_date Total_count Count_Diff_1 Count_Diff_2 Count_Diff_3 Average_Diff_1 Average_Diff_2 Average_Diff_3 Per_Diff_1 Per_Diff_2 Per_Diff_3
# <chr> <int> <int> <int> <int> <chr> <chr> <chr> <chr> <chr> <chr>
#1 Mar-21 (W2) 1288 519 624 145 2.75 hours 2.59 hours 2.26 Days 40.3% 48.4% 11.3%
#2 Mar-21 (W1) 1079 508 505 66 6.04 hours 7.78 hours 5.56 Days 47.1% 46.8% 6.1%
#3 Feb-21 (W5) 53 29 22 2 10.35 hours 6.47 hours 6.77 Days 54.7% 41.5% 3.8%

关于r - 如何将 difftime 的值从小时数转换为天数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66519958/

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