Reputation: 783
I have a dataframe which has column names as shown in the dput
data below.
structure(list(mosales = c(1L, 1L, 1L, 12L, 1L), sale123 = c(14.86,
8.97, 6.44, 463.61, 15.94), totsales = c(1L, 1L, 1L,
30L, 1L), totqty = c(1L, 1L, 1L, 34L, 2L), unqsales = c(1L,
1L, 1L, 6L, 2L), x1_rank_1 = c(1L, 1L, 1L, 0L, 1L), x1_rank_4 = c(0L,
0L, 0L, 1L, 0L), x1_rank_3 = c(0L, 0L, 0L, 0L, 0L), x1_rank_2 = c(0L,
0L, 0L, 0L, 0L), x2_rank_2 = c(1L, 1L, 0L, 0L, 1L), x2_rank_1 = c(0L,
0L, 1L, 0L, 0L), x2_rank_5 = c(0L, 0L, 0L, 1L, 0L), x2_rank_4 = c(0L,
0L, 0L, 0L, 0L), x2_rank_3 = c(0L, 0L, 0L, 0L, 0L), x3_rank_1 = c(1L,
1L, 1L, 0L, 1L), x3_rank_4 = c(0L, 0L, 0L, 1L, 0L), x3_rank_3 = c(0L,
0L, 0L, 0L, 0L), x3_rank_2 = c(0L, 0L, 0L, 0L, 0L), x4_rank_1 = c(1L,
1L, 1L, 0L, 0L), x4_rank_5 = c(0L, 0L, 0L, 1L, 0L), x4_rank_2 = c(0L,
0L, 0L, 0L, 1L), x4_rank_4 = c(0L, 0L, 0L, 0L, 0L), x4_rank_3 = c(0L,
0L, 0L, 0L, 0L), x5_rank_1 = c(1L, 1L, 1L, 0L, 0L), x5_rank_4 = c(0L,
0L, 0L, 1L, 0L), x5_rank_2 = c(0L, 0L, 0L, 0L, 1L), x5_rank_3 = c(0L,
0L, 0L, 0L, 0L)), row.names = c(36L, 41L, 72L, 79L, 137L), class = "data.frame")
What I am trying to do is to replace columns names, which start with x1_rank, x2_rank .. and so forth
, by the names such that "x1_rank" should be replaced with mosales, "x2_rank" should be replaced with sales123, "x3_rank" should be replaced with "totsales", "x4_rank" should be replaced with "totqty" and "x5_rank" should be replaced with "unqsales".
So the final column names would look like:
mosales, sale123, totsales, totqty, unqsales, mosales_1, mosales_2,... sale123_1, sale123_2,... totsales_1, totsales_2,... totqty_1, totqty_2,... unqsales_1, unqsales_2,...
I tried using a for loop and gsub, as in below. This code ran without any errors but I didn't get what I was looking for. Not sure where is the error.
df1 <- df
z <- names(df)
for (i in 1:length(z)){
gsub(paste0("x",i,"_rank"), z[i], names(df1))
}
df is dataframe which can be created using above dput data.
Upvotes: 0
Views: 39
Reputation: 10437
String manipulation functions in the stringi
package are vectorized over pattern
, replacement
, and (optionally) string
. This is very convenient for your case:
library(stringi)
orig <- c(stri_c("x", 1:5, "_rank"))
repl <- c("mosales", "sales123", "totsales", "totqty", "unqsales")
names(df) <- stri_replace_all_fixed(names(df), orig, repl,
vectorize_all = FALSE)
Upvotes: 2
Reputation: 8364
You can use this reg \\bx1_rank[a-zA-Z]*
.
This will match x1_rank
at the start of the string.
gsub
lets you replace the found pattern with what you want. Repeat for all the cases you need.
In a loop:
repl <- c("mosales", "sales123", "totsales", "totqty", "unqsales")
for (i in 1:5) {
p = paste0("\\b^x", i, "_rank[a-zA-Z]*")
colnames(d) = gsub(pattern=p, repl[i], colnames(d))
}
colnames(d)
#
# [1] "mosales" "sale123" "totsales" "totqty" "unqsales" "mosales_1" "mosales_4"
# [8] "mosales_3" "mosales_2" "sales123_2" "sales123_1" "sales123_5" "sales123_4" "sales123_3"
# [15] "totsales_1" "totsales_4" "totsales_3" "totsales_2" "totqty_1" "totqty_5" "totqty_2"
# [22] "totqty_4" "totqty_3" "unqsales_1" "unqsales_4" "unqsales_2" "unqsales_3"
Upvotes: 1