R 如何将difftime的值从小时转换为天?
我有一个看起来像的数据框R 如何将difftime的值从小时转换为天?,r,dataframe,dplyr,tidyverse,R,Dataframe,Dplyr,Tidyverse,我有一个看起来像的数据框 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,
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_1
、Average_2
和Average_3
列中的值大于24小时,我想转换天的值,并将peru Diff_1
、peru Diff_2
和peru Diff_3
转换为带%符号的2个小数点
另外,如果我能设法按降序排列格式\ u日期
,那就太好了。现在它的名字是Feb-21(W5)
,Mar-21(W1)
和Mar-21(W2)
它应该是Mar-21(W2)
,Mar-21(W1)
和Feb-21(W5)
所需df我们可以在
以
'Average'开头的列上使用来转换大于24的值,然后除以24,附加'Days'作为后缀,或者通过附加'hours'作为后缀返回。类似地,以
“Per”开头的列可以通过标签_百分比
从比例
格式化为百分比。然后,我们在转换到yearmon
类(从zoo::as.yearmon
)后,在desc
中安排结束顺序中的'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%
#一个tible:3 x 11
#格式\日期总计\计数计数\差异\计数\差异\计数\差异\ 2计数\差异\ 3平均\差异\ 1平均\差异\ 2平均\差异\ 3每\差异\ 1每\差异\ 2每\差异\ 3
#
#21年3月1日(W2)1288519624145 2.75小时2.59小时2.26天40.3%48.4%11.3%
#21年3月2日(W1)1079 508 505 66 6.04小时7.78小时5.56天47.1%46.8%6.1%
#21年2月3日(W5)53 29 22 10.35小时6.47小时6.77天54.7%41.5%3.8%
您可以使用if
将案件处理中difftime类的单位更改为天。对于百分比,请尝试formatC
d[6:8] <- lapply(d[6:8], function(x) {if (all(x > 24)) units(x) <- "days";round(x, 2)})
d[9:11] <- lapply(d[9:11], function(x) paste0(formatC(x*100, 1, format="f"), "%"))
d <- d[order(d$format_date, decreasing=T), ]
@akrun:只要hours
在其应显示的任何平均列中超过24天,则预期转换为。例如,162.66625小时
到6.77天
非常感谢,它成功了。如果我能按降序排列format\u date
列,只需再提供一个帮助。@SophiaWilson我想更新应该会有帮助。是的,它成功了。只有几个星期没有安排好。理想情况下,Mar-21(W2)
应该是第一行。@SophiaWilson好的,我会告诉你,它工作得很好。再次非常感谢:)
# 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%
d[6:8] <- lapply(d[6:8], function(x) {if (all(x > 24)) units(x) <- "days";round(x, 2)})
d[9:11] <- lapply(d[9:11], function(x) paste0(formatC(x*100, 1, format="f"), "%"))
d <- d[order(d$format_date, decreasing=T), ]
d
# format_date Total_count Count_Diff_1 Count_Diff_2 Count_Diff_3 Average_Diff_1
# 3 Mar-21 (W2) 1288 519 624 145 2.75 hours
# 2 Mar-21 (W1) 1079 508 505 66 6.04 hours
# 1 Feb-21 (W5) 53 29 22 2 10.35 hours
# Average_Diff_2 Average_Diff_3 Per_Diff_1 Per_Diff_2 Per_Diff_3
# 3 2.59 hours 2.26 days 40.3% 48.4% 11.3%
# 2 7.78 hours 5.56 days 47.1% 46.8% 6.1%
# 1 6.47 hours 6.77 days 54.7% 41.5% 3.8%