I have a dataset that includes a bunch of variables with various suffixes that I want to make into prefixes. The dataset also includes some variables without any suffixes. Something like:
df <- data.frame(
home_loc = rnorm(5),
work_loc = rnorm(5),
x1 = rnorm(5),
walk_act = rnorm(5),
bike_act = rnorm(5),
x2 = rnorm(5),
happy_yest = rnorm(5),
sad_yest = rnorm(5)
)
I was able to come up with the following solution:
suff_to_pre <- function(x, suffix, prefix) {
for (i in seq_along(names(x))) {
if (grepl(suffix, names(x)[i])) {
names(x)[i] <- sub(suffix, "", names(x)[i])
names(x)[i] <- paste0(prefix, names(x)[i])
}
}
names(x)
}
names(df) <- suff_to_pre(df, suffix = "_loc", prefix = "loc_")
names(df) <- suff_to_pre(df, suffix = "_act", prefix = "act_")
names(df) <- suff_to_pre(df, suffix = "_yest", prefix = "yest_")
names(df)
[1] "loc_home" "loc_work" "x1" "act_walk" "act_bike" "x2" "yest_happy"
[8] "yest_sad"
But, Iām not feeling very satisfied with it. Specifically, I would really like a way to get the same result using dplyr. I found this and this, which got me to:
a <- df %>%
select(ends_with("_loc")) %>%
setNames(sub("_loc", "", names(.))) %>%
setNames(paste0("loc_", names(.)))
b <- df %>%
select(ends_with("_act")) %>%
setNames(sub("_act", "", names(.))) %>%
setNames(paste0("act_", names(.)))
c <- df %>%
select(ends_with("_yest")) %>%
setNames(sub("_yest", "", names(.))) %>%
setNames(paste0("yest_", names(.)))
df <- cbind(
select(df, x1, x2), a, b, c
)
Which is obviously not ideal. I was hoping someone out there suggest a more elegant solution using dplyr.
Edit
@docendo discimus and @zx8754 gave really helpful answers, but I should have been more explicit. I also have variables that include underscores, but are not suffixes that I want to change into prefixes.
For Example (see free_time):
df <- data.frame(
home_loc = rnorm(5),
work_loc = rnorm(5),
x_1 = rnorm(5),
walk_act = rnorm(5),
bike_act = rnorm(5),
x_2 = rnorm(5),
happy_yest = rnorm(5),
sad_yest = rnorm(5),
free_time = rnorm(5)
)