Note that the values of my column time
in the output table are rounded, but I would like to leave the values two decimal places after the comma, how to adjust this in the code below?
library(dplyr)
Test <- structure(list(date1 = as.Date(c("2021-11-01","2021-11-01","2021-11-01","2021-11-01")),
date2 = as.Date(c("2021-10-22","2021-10-22","2021-10-28","2021-10-30")),
Week = c("Friday", "Friday", "Thursday", "thursday"),
Category = c("FDE", "FDE", "FDE", "FDE"),
time = c(4, 6, 6, 3)), class = "data.frame",row.names = c(NA, -4L))
Test<-Test %>%
group_by(Week = tools::toTitleCase(Week), Category) %>%
summarise(time = mean(time, na.rm = TRUE), .groups = 'drop')
Test <- transform(Test, time = round(time))
> Test
Week Category time
1 Friday FDE 5
2 Thursday FDE 4
CodePudding user response:
An alternative to Onyambu's suggestion is:
Test <- transform(Test, time = format(round(time, digits = 2), nsmall = 2))
The nsmall argument of format sets the minimum number of digits to the right of the decimal.