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

Creating a character vector with multiple sequences of data

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

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 :

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