创建具有多个数据序列的字符向量

Creating a character vector with multiple sequences of data

我正在尝试使用字符向量重命名一些数据文件。每个文件包含对应于月份和年份的数据。我想这样重命名文件:“month_year”。

我已经成功地创建了我想要的字符向量(见下文),但我想知道是否有更简洁的方法来完成这个。必须有一种方法可以在一个函数中执行此操作,是吗?最好在基数 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)))

这是一个dplyr解决方案:

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"

我们可以用rep

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

-使用 OP 的代码进行测试

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

您可以在 outer 中使用 paste

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

给予

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

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

为了完整起见,这里有一个“one-liner”,它使用 [=13= 中的 CJ() (cross join) 函数] 包裹:

data.table::CJ(month.name[6:9], 2014:2017)[, paste(V1, V2, sep = "_")]
 [1] "August_2014"    "August_2015"    "August_2016"    "August_2017"    "July_2014"      "July_2015"     
 [7] "July_2016"      "July_2017"      "June_2014"      "June_2015"      "June_2016"      "June_2017"     
[13] "September_2014" "September_2015" "September_2016" "September_2017"

请注意,为了演示,年份范围已缩小。

输出按 中 OP 的要求按字母顺序排序,因为 CJ() 默认对其输出排序。