Home > Mobile >  Creating a character vector with multiple sequences of data
Creating a character vector with multiple sequences of data

Time:04-27

I am trying to rename some data files using a character vector. Each file contains data corresponding to a month and year. I want to rename the files as such: "month_year".

I have successfully created the character vector as I want (see below) but I was wondering if there was a more succinct way to accomplish this. There must be a way to do this in one function, yes? Preferably in base R.

data.names <- expand.grid(month = c("June", "July", "August", "September"),
                          year = c(1984:2017),
                          stringsAsFactors = FALSE)
data.names <- as.character(c(paste0(data.names$month, "_", data.names$year)))

CodePudding user response:

We may use rep

out <- paste0(month.name[6:9],  "_", rep(1984:2017, each = 4))

-testing with OP's code

> out_prev <- as.character(c(paste0(data.names$month, "_", data.names$year)))
> identical(out, out_prev)
[1] TRUE

CodePudding user response:

Here is a dplyr solution:

library(dplyr)

vector_data.names <- data.names %>% 
  mutate(data.names = paste(month, year, sep = "_")) %>% 
  pull(data.names)
vector_data.names
  [1] "June_1984"      "July_1984"      "August_1984"    "September_1984" "June_1985"     
  [6] "July_1985"      "August_1985"    "September_1985" "June_1986"      "July_1986"     
 [11] "August_1986"    "September_1986" "June_1987"      "July_1987"      "August_1987"   
 [16] "September_1987" "June_1988"      "July_1988"      "August_1988"    "September_1988"
 [21] "June_1989"      "July_1989"      "August_1989"    "September_1989" "June_1990"     
 [26] "July_1990"      "August_1990"    "September_1990" "June_1991"      "July_1991"     
 [31] "August_1991"    "September_1991" "June_1992"      "July_1992"      "August_1992"   
 [36] "September_1992" "June_1993"      "July_1993"      "August_1993"    "September_1993"
 [41] "June_1994"      "July_1994"      "August_1994"    "September_1994" "June_1995"     
 [46] "July_1995"      "August_1995"    "September_1995" "June_1996"      "July_1996"     
 [51] "August_1996"    "September_1996" "June_1997"      "July_1997"      "August_1997"   
 [56] "September_1997" "June_1998"      "July_1998"      "August_1998"    "September_1998"
 [61] "June_1999"      "July_1999"      "August_1999"    "September_1999" "June_2000"     
 [66] "July_2000"      "August_2000"    "September_2000" "June_2001"      "July_2001"     
 [71] "August_2001"    "September_2001" "June_2002"      "July_2002"      "August_2002"   
 [76] "September_2002" "June_2003"      "July_2003"      "August_2003"    "September_2003"
 [81] "June_2004"      "July_2004"      "August_2004"    "September_2004" "June_2005"     
 [86] "July_2005"      "August_2005"    "September_2005" "June_2006"      "July_2006"     
 [91] "August_2006"    "September_2006" "June_2007"      "July_2007"      "August_2007"   
 [96] "September_2007" "June_2008"      "July_2008"      "August_2008"    "September_2008"
[101] "June_2009"      "July_2009"      "August_2009"    "September_2009" "June_2010"     
[106] "July_2010"      "August_2010"    "September_2010" "June_2011"      "July_2011"     
[111] "August_2011"    "September_2011" "June_2012"      "July_2012"      "August_2012"   
[116] "September_2012" "June_2013"      "July_2013"      "August_2013"    "September_2013"
[121] "June_2014"      "July_2014"      "August_2014"    "September_2014" "June_2015"     
[126] "July_2015"      "August_2015"    "September_2015" "June_2016"      "July_2016"     
[131] "August_2016"    "September_2016" "June_2017"      "July_2017"      "August_2017"   
[136] "September_2017"

CodePudding user response:

You could use paste in outer.

r <- as.vector(outer(month.name[6:9], 1984:2017, paste, sep='_'))

Gives

head(r)
# [1] "June_1984"      "July_1984"      "August_1984"    "September_1984" "June_1985"      "July_1985"     

stopifnot(all.equal(r, data.names))
  • Related