Home > Back-end >  Joining the numbers together from columns in R
Joining the numbers together from columns in R

Time:09-03

I simplified the data to demonstrate what I want to do. data looks like this:

data<-data.frame(date=c(20220823,20220801,20220904,20220906),
                 wk=c(4,1,2,2))

For example, 20220823 means August 23th, 2022. wk means week. What I want to do now is make a new column date2 which represents the year,month, and week. So my expected output should be as follows:

data<-data.frame(date2=c(2022084,2022081,2022092,2022092))

CodePudding user response:

Is this what you want?

library(dplyr)

 data |> 
  mutate(date2 = paste0(substr(date, 1,6), wk))

Output:

      date wk   date2
1 20220823  4 2022084
2 20220801  1 2022081
3 20220904  2 2022092
4 20220906  2 2022092
  • Related