I am trying to rename the column names based on combination of two vectors.
vector_a <- seq(5, 50, 5)
vector_b <- seq(2, 10, 1)
df <- as.data.frame(matrix(0, nrow = 100, ncol = length(vector_a) * length(vector_b)))
I am trying to obtain column names as
k=5,f=2
, k=10,f=2
, k=15,f=2
,..., k=50,f=2
k=5,f=3
, k=10,f=3
, k=15,f=3
,..., k=50,f=3
...
...
...
k=5,f=10
, k=10,f=10
, k=15,f=10
,..., k=50,f=10
without using for
loop.
I tried the following code; however, I didn't receive what I wanted.
paste0(rep(paste0(paste0(paste0(rep("k",length(vector_b)), "=", as.character(vector_b)), ",", "f"), "="), 10), as.character(vector_a))
Thank you in advance.
Perhaps, we can use outer
to replicate the 'vector_a', 'vector_b' to return a vector of names
t(outer(vector_a, vector_b, FUN = function(x, y) sprintf("k=%d, f = %d", x, y)))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] "k=5, f = 2" "k=10, f = 2" "k=15, f = 2" "k=20, f = 2" "k=25, f = 2" "k=30, f = 2" "k=35, f = 2" "k=40, f = 2"
# [2,] "k=5, f = 3" "k=10, f = 3" "k=15, f = 3" "k=20, f = 3" "k=25, f = 3" "k=30, f = 3" "k=35, f = 3" "k=40, f = 3"
# [3,] "k=5, f = 4" "k=10, f = 4" "k=15, f = 4" "k=20, f = 4" "k=25, f = 4" "k=30, f = 4" "k=35, f = 4" "k=40, f = 4"
# [4,] "k=5, f = 5" "k=10, f = 5" "k=15, f = 5" "k=20, f = 5" "k=25, f = 5" "k=30, f = 5" "k=35, f = 5" "k=40, f = 5"
# [5,] "k=5, f = 6" "k=10, f = 6" "k=15, f = 6" "k=20, f = 6" "k=25, f = 6" "k=30, f = 6" "k=35, f = 6" "k=40, f = 6"
# [6,] "k=5, f = 7" "k=10, f = 7" "k=15, f = 7" "k=20, f = 7" "k=25, f = 7" "k=30, f = 7" "k=35, f = 7" "k=40, f = 7"
# [7,] "k=5, f = 8" "k=10, f = 8" "k=15, f = 8" "k=20, f = 8" "k=25, f = 8" "k=30, f = 8" "k=35, f = 8" "k=40, f = 8"
# [8,] "k=5, f = 9" "k=10, f = 9" "k=15, f = 9" "k=20, f = 9" "k=25, f = 9" "k=30, f = 9" "k=35, f = 9" "k=40, f = 9"
# [9,] "k=5, f = 10" "k=10, f = 10" "k=15, f = 10" "k=20, f = 10" "k=25, f = 10" "k=30, f = 10" "k=35, f = 10" "k=40, f = 10"
# [,9] [,10]
# [1,] "k=45, f = 2" "k=50, f = 2"
# [2,] "k=45, f = 3" "k=50, f = 3"
# [3,] "k=45, f = 4" "k=50, f = 4"
# [4,] "k=45, f = 5" "k=50, f = 5"
# [5,] "k=45, f = 6" "k=50, f = 6"
# [6,] "k=45, f = 7" "k=50, f = 7"
# [7,] "k=45, f = 8" "k=50, f = 8"
# [8,] "k=45, f = 9" "k=50, f = 9"
# [9,] "k=45, f = 10" "k=50, f = 10"
You can use expand.grid
/ crossing
/ expand_grid
to get all possible combinations of vector_a
and vector_b
.
You can then paste
the first column with the second one to generate the names.
temp <- tidyr::crossing(vector_a, vector_b)
temp <- expand.grid(vector_a, vector_b)
temp <- tidyr::expand_grid(vector_a, vector_b)
paste0('k = ', temp[[1]], ', f = ', temp[[2]])
#[1] "k = 5, f = 2" "k = 5, f = 3" "k = 5, f = 4" "k = 5, f = 5"
#[5] "k = 5, f = 6" "k = 5, f = 7" "k = 5, f = 8" "k = 5, f = 9"
#[9] "k = 5, f = 10" "k = 10, f = 2" "k = 10, f = 3" "k = 10, f = 4"
#.....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.