Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Joining the numbers together from columns in R

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))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading