I have a database that has a Week
column. Weekday names are Sunday, Monday, Tuesday and only on. However, I would like to automatically change this to the following: Week = c("monday day","tuesday day","wednesday day","thursday day","friday day" "saturday","sunday day" )
.In this case below, as my database only has two days of the week, only Monday and Sunday will be changed. How to adjust this?
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-18","2021-10-18","2021-10-28","2021-10-30")),
Week = c("Monday", "monday", "Sunday", "Sunday"),
Category = c("FDE", "FDE", "FDE", "FDE"),
time = c(4, 6, 6, 3)), class = "data.frame",row.names = c(NA, -4L))
Here's two ways to do this - one in Base R and one in tidyverse.
# Base R
Test_Orig <- 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-18","2021-10-18","2021-10-28","2021-10-30")),
Week = c("Monday", "monday", "Sunday", "Sunday"),
Category = c("FDE", "FDE", "FDE", "FDE"),
time = c(4, 6, 6, 3)), class = "data.frame",row.names = c(NA, -4L))
Test$Week <- paste(Test$Week, "day")
Test
#> date1 date2 Week Category time
#> 1 2021-11-01 2021-10-18 Monday day FDE 4
#> 2 2021-11-01 2021-10-18 monday day FDE 6
#> 3 2021-11-01 2021-10-28 Sunday day FDE 6
#> 4 2021-11-01 2021-10-30 Sunday day FDE 3
# tidyverse
library(tidyverse)
Test <- Test_Orig
Test <- Test %>%
mutate(
Week=str_c(Week, "day", sep=" ")
)
Test
#> date1 date2 Week Category time
#> 1 2021-11-01 2021-10-18 Monday day FDE 4
#> 2 2021-11-01 2021-10-18 monday day FDE 6
#> 3 2021-11-01 2021-10-28 Sunday day FDE 6
#> 4 2021-11-01 2021-10-30 Sunday day FDE 3
Created on 2021-12-27 by the reprex package (v2.0.1)
1 Like
system
Closed
January 3, 2022, 9:38pm
3
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.