-1

I'm a beginner to R. What I want to do is join numbers together. I made a data as follows:

data<-data.frame(year=c(2020,2021,2022),month=c(10,11,12))

My expected output is as follows:

data=data.frame(year=c(2020,2021,2022),month=c(10,11,12),year_month=c(202010,202111,202212))

year_month is the column joining year and month together. How can I do this?

philipxy
  • 14,867
  • 6
  • 39
  • 83
Bae
  • 97
  • 1
  • 6

1 Answers1

2

You could concatenate the columns using paste0 like this:

data<-data.frame(year=c(2020,2021,2022),month=c(10,11,12))
data$year_month <- do.call(paste0, data)
data
#>   year month year_month
#> 1 2020    10     202010
#> 2 2021    11     202111
#> 3 2022    12     202212

Created on 2022-07-30 by the reprex package (v2.0.1)

Quinten
  • 35,235
  • 5
  • 20
  • 53