You can create two data frames: one where the second element is a number, and the second is the same as the first element, and then rbind those. The example is below, please note that I have limited your example data to illustrate.
Letters <- LETTERS[1:3] Numbers <- c(1,2) df1 = expand.grid(v1=Letters,v3=Numbers,stringsAsFactors = F) df1$v2 = df1$v1 df1 = df1[,c('v1','v2','v3')] df2 = expand.grid(v1=Letters,v2=as.character(Numbers),v3=Numbers, stringsAsFactors = F) df = rbind(df1,df2)
Output:
> df v1 v2 v3 1 AA 1 2 BB 1 3 CC 1 4 AA 2 5 BB 2 6 CC 2 7 A 1 1 8 B 1 1 9 C 1 1 10 A 2 1 11 B 2 1 12 C 2 1 13 A 1 2 14 B 1 2 15 C 1 2 16 A 2 2 17 B 2 2 18 C 2 2
Hope this helps!
Although both answers are very fast and Parfait's solution is a good solution to your problem, and I certainly don't want to discredit his answer, I find it useful to note that creating additional combinations and subsets will become a more serious problem if your data is larger. The following is a comparative test.
Letters <- c(LETTERS[1:26],letters[1:4]) Numbers <- seq(30) AlphaNumeric <- c(Letters, Numbers) f_flo <- function() { df1 = expand.grid(v1=Letters,v3=Numbers,stringsAsFactors = F) df1$v2 = df1$v1 df1 = df1[,c('v1','v2','v3')] df2 = expand.grid(v1=Letters,v2=as.character(Numbers),v3=Numbers, stringsAsFactors = F) df = rbind(df1,df2) } f_parfait <- function() { df <- expand.grid(x = Letters, y = AlphaNumeric, z = Numbers, stringsAsFactors = FALSE) sub <- subset(df, (x == y | grepl("[0-9]", y)) & grepl("[0-9]", z) ) sub <- with(sub, sub[order(x, y, z),]) # SORT DATAFRAME rownames(sub) <- NULL # RESET ROWNAMES } library(dplyr) one_letter <- function(l) { expand.grid(l, c(l, Numbers), Numbers, stringsAsFactors = FALSE) } f_stibu <- function(){ df <- bind_rows(lapply(Letters, one_letter)) } library(microbenchmark) library(ggplot2) run_times = microbenchmark(f_flo(),f_parfait(),f_stibu()) autoplot(run_times)
Results:
Unit: milliseconds expr min lq mean median uq max neval cld f_flo() 1.900719 2.047591 3.666935 2.314258 3.922053 78.74793 100 a f_parfait() 138.028364 142.529904 152.876116 144.159444 146.835958 246.92318 100 b f_stibu() 4.130464 4.333130 5.169664 4.585028 6.209233 10.23139 100 a
