How do I add a prefix to several variable names using dplyr?
Indeed, you can use rename_
(NSE rename
itself doesn’t work):
data %>% rename_(.dots = setNames(names(.), paste0('cars.', names(.))))
… but honestly, why? Just assigning names directly is shorter and more readable:
data %>% setNames(paste0('cars.', names(.)))
Adding prefix or suffix to most data.frame variable names in piped R workflow
You can pass functions to rename_at, so do
means14 <- dat14 %>%
group_by(class) %>%
select(-ID) %>%
summarise_all(funs(mean(.))) %>%
rename_at(vars(-class),function(x) paste0(x,"_2014"))
Rename all columns except id column by adding a prefix using dplyr
library(dplyr)
df %>%
dplyr::rename_with(~ paste0("source_", .), -id)
The third argument to rename_with
is .cols
, where you can use tidyselect syntax to select the columns. Here -id
excludes this column.
Per the comments the .
syntax is a cleaner/simpler style than an writing an anonymous function, but you could accomplish this equivalently as:
df %>%
dplyr::rename_with(function(x) paste0("source_", x), -id)
Add a prefix to column names
You have misread the help file. Here's the argument to look at:
do.NULL
: logical. If FALSE
and names are NULL
, names are created.
Notice the and in that description. Your names are no longer NULL
, so using prefix won't work.
Instead, use something like this:
> m2 <- cbind(1,1:4)
> colnames(m2) <- c("x","Y")
> colnames(m2) <- paste("Sub", colnames(m2), sep = "_")
> m2
Sub_x Sub_Y
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
Adding a suffix to a selection of column names in tidyverse
You may select columns in rename_with
-
library(dplyr)
df %>% rename_with(~paste0("a", .x), c(x, y))
# ax ay z
#1 1 3 5
#2 2 4 6
Adding a prefix to a column name using the value of different column using dplyr
Here is a workaround solution. It only looks at the first row of time_period
and takes what ever value it finds there:
df %>% {rename_at(., vars(sum), function(x) paste(pull(., time_period)[1], x, sep = "_"))}
# A tibble: 3 x 2
year_sum time_period
<dbl> <chr>
1 150 year
2 175 year
3 200 year
Adding a prefix to certain column names
Try this:
colnames(m2)[1] <- paste0("Sub", "_", colnames(m2)[1])
# or if you prefer paste
#colnames(m2)[1] <- paste("Sub", colnames(m2)[1], sep = "_")
How to rename mutliples columns names (with prefix) according to respective dataframe name [R]
First, refer to elements of the list with double square brackets, like so List_df_EU[[i]]
(List_df_EU[i]
is a sub-list of 1 element, not the element itself).
Second, we could create List_df_EU
with tibble::lst()
instead of list()
, so that elements are automatically named. Then, "AS"
can be replaced with names(List_df_EU)[i]
.
List_df_EU <- tibble::lst(....)
for(i in 1:length(List_df_EU)) {
colnames(List_df_EU[[i]]) <- paste(
names(List_df_EU)[i], colnames(List_df_EU[[i]]), sep = "_")
}
Edit
To allow the subsequent join on OBJECTID
, we could rename all columns but OBJECTID
, for instance using dplyr
that has a nice interface for this:
for(i in 1:length(List_df_EU)) {
List_df_EU[[i]] <- dplyr::rename_with(
List_df_EU[[i]],
~ paste(names(List_df_EU)[i], .x, sep = "_"),
.cols = - OBJECTID
)
}
How to use rename_with() in pipe to change all column names from vector
It doesn't seem like rename_with
is the right function for this. I guess you could do
iris %>%
rename_with(~newcolnames)
as kind of a hack. You can also use rename
iris %>%
rename(!!!setNames(names(.), newcolnames))
But the setNames
method just seems much more apt.
Related Topics
Multiple Boxplots Using Ggplot
Installing R 3.5.0 with --Enable-R-Shlib
How to Do Conditional Grouping of Data in R
R: How to Draw a Line with Multiple Arrows in It
How to Run Lm Regression for Every Column in R
Grouping & Visualizing Cumulative Features in R
How to Remove Rows That Have Only 1 Combination for a Given Id
Unnest a List Column Directly into Several Columns
Detect Non Ascii Characters in a String
Get the Number of Lines in a Text File Using R
How to Remove Empty Data Frames from a List
Using Legend with Stat_Function in Ggplot2