Reputation: 430
I'd like to put names in datasets automatically using paste
or paste0
. For example, if we consider a dataset with three variables, we have six columns and the expected result is
dta <- data.frame(matrix(1:60, ncol = 6))
names(dta) <- c('X1_dim1', 'X1_dim2', 'X2_dim1', 'X2_dim2', 'X3_dim1', 'X3_dim2')
Upvotes: 0
Views: 60
Reputation: 388817
Using rep
paste0("X", rep(1:ncol(dta), each = 2, length.out = ncol(dta)), "_dim", 1:2)
#[1] "X1_dim1" "X1_dim2" "X2_dim1" "X2_dim2" "X3_dim1" "X3_dim2"
rep
generates sequence of length ncol(dta)
with each element repeated twice
rep(1:ncol(dta), each = 2, length.out = ncol(dta))
#[1] 1 1 2 2 3 3
Upvotes: 3