Home > Software engineering >  How to construct complex strings in R
How to construct complex strings in R

Time:05-17

the target string vector I want is like this:

a1=paste0('2001',month.abb)
a2=paste0('2002',month.abb)
a3=paste0('2003',month.abb)
a4=paste0('2004',month.abb)
a5=paste0('2005',month.abb)
a6=paste0('2006',month.abb)
a7=paste0('2007',month.abb)
a8=paste0('2008',month.abb)
a9=paste0('2009',month.abb)
a10=paste0('2010',month.abb)

c(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10)

Is there an easier way to do it?

CodePudding user response:

You can pass the sequence 2001 to 2010 to sapply, and tell it to paste0 the vector month.abb to each element. This actually produces a 12 x 10 matrix of the strings, so if you want them as a vector, just wrap the call in c

c(sapply(2001:2010, paste0, month.abb))
#>   [1] "2001Jan" "2001Feb" "2001Mar" "2001Apr" "2001May" "2001Jun" "2001Jul"
#>   [8] "2001Aug" "2001Sep" "2001Oct" "2001Nov" "2001Dec" "2002Jan" "2002Feb"
#>  [15] "2002Mar" "2002Apr" "2002May" "2002Jun" "2002Jul" "2002Aug" "2002Sep"
#>  [22] "2002Oct" "2002Nov" "2002Dec" "2003Jan" "2003Feb" "2003Mar" "2003Apr"
#>  [29] "2003May" "2003Jun" "2003Jul" "2003Aug" "2003Sep" "2003Oct" "2003Nov"
#>  [36] "2003Dec" "2004Jan" "2004Feb" "2004Mar" "2004Apr" "2004May" "2004Jun"
#>  [43] "2004Jul" "2004Aug" "2004Sep" "2004Oct" "2004Nov" "2004Dec" "2005Jan"
#>  [50] "2005Feb" "2005Mar" "2005Apr" "2005May" "2005Jun" "2005Jul" "2005Aug"
#>  [57] "2005Sep" "2005Oct" "2005Nov" "2005Dec" "2006Jan" "2006Feb" "2006Mar"
#>  [64] "2006Apr" "2006May" "2006Jun" "2006Jul" "2006Aug" "2006Sep" "2006Oct"
#>  [71] "2006Nov" "2006Dec" "2007Jan" "2007Feb" "2007Mar" "2007Apr" "2007May"
#>  [78] "2007Jun" "2007Jul" "2007Aug" "2007Sep" "2007Oct" "2007Nov" "2007Dec"
#>  [85] "2008Jan" "2008Feb" "2008Mar" "2008Apr" "2008May" "2008Jun" "2008Jul"
#>  [92] "2008Aug" "2008Sep" "2008Oct" "2008Nov" "2008Dec" "2009Jan" "2009Feb"
#>  [99] "2009Mar" "2009Apr" "2009May" "2009Jun" "2009Jul" "2009Aug" "2009Sep"
#> [106] "2009Oct" "2009Nov" "2009Dec" "2010Jan" "2010Feb" "2010Mar" "2010Apr"
#> [113] "2010May" "2010Jun" "2010Jul" "2010Aug" "2010Sep" "2010Oct" "2010Nov"
#> [120] "2010Dec"

If you would prefer spaces between years and months, use paste instead of paste0.

An alternative is to generate a sequence of months and have them formatted this way:

 format(seq(as.Date("2001-01-01"), as.Date("2010-12-01"), "month"), "%Y%b")
#>   [1] "2001Jan" "2001Feb" "2001Mar" "2001Apr" "2001May" "2001Jun" "2001Jul"
#>   [8] "2001Aug" "2001Sep" "2001Oct" "2001Nov" "2001Dec" "2002Jan" "2002Feb"
#>  [15] "2002Mar" "2002Apr" "2002May" "2002Jun" "2002Jul" "2002Aug" "2002Sep"
#>  [22] "2002Oct" "2002Nov" "2002Dec" "2003Jan" "2003Feb" "2003Mar" "2003Apr"
#>  [29] "2003May" "2003Jun" "2003Jul" "2003Aug" "2003Sep" "2003Oct" "2003Nov"
#>  [36] "2003Dec" "2004Jan" "2004Feb" "2004Mar" "2004Apr" "2004May" "2004Jun"
#>  [43] "2004Jul" "2004Aug" "2004Sep" "2004Oct" "2004Nov" "2004Dec" "2005Jan"
#>  [50] "2005Feb" "2005Mar" "2005Apr" "2005May" "2005Jun" "2005Jul" "2005Aug"
#>  [57] "2005Sep" "2005Oct" "2005Nov" "2005Dec" "2006Jan" "2006Feb" "2006Mar"
#>  [64] "2006Apr" "2006May" "2006Jun" "2006Jul" "2006Aug" "2006Sep" "2006Oct"
#>  [71] "2006Nov" "2006Dec" "2007Jan" "2007Feb" "2007Mar" "2007Apr" "2007May"
#>  [78] "2007Jun" "2007Jul" "2007Aug" "2007Sep" "2007Oct" "2007Nov" "2007Dec"
#>  [85] "2008Jan" "2008Feb" "2008Mar" "2008Apr" "2008May" "2008Jun" "2008Jul"
#>  [92] "2008Aug" "2008Sep" "2008Oct" "2008Nov" "2008Dec" "2009Jan" "2009Feb"
#>  [99] "2009Mar" "2009Apr" "2009May" "2009Jun" "2009Jul" "2009Aug" "2009Sep"
#> [106] "2009Oct" "2009Nov" "2009Dec" "2010Jan" "2010Feb" "2010Mar" "2010Apr"
#> [113] "2010May" "2010Jun" "2010Jul" "2010Aug" "2010Sep" "2010Oct" "2010Nov"
#> [120] "2010Dec"

Created on 2022-05-16 by the reprex package (v2.0.1)

  • Related