View source: R/wrangling-utils.R
rename_with_seq | R Documentation |
This function will rename a selection of columns as, for
example, var_1
, var_2
, var_2
... var_10
, where the prefix, var
in
this example, is arbitrary.
rename_with_seq(data_df, col_selector, prefix = "var")
data_df |
A data frame |
col_selector |
A tidy selector, e.g. |
prefix |
The prefix for the sequence, e.g. 'drug' to produce names like
|
If we had, for example, a data frame where columns were the names of
drugs and we wanted to rename these columns something like drug_1
,
drug_2
, ..., this would be easy to do with rename if there
were just a few columns to rename. When there are more than just a few,
individual renaming is somewhat tedious and error prone. We can use
rename_with to do this in one operation. However, the code
for doing so is not very simple and would require some proficiency in R and
tidyverse
. This function is essentially just a wrapper to a rename_with
function to allow the renaming to be done in one simple command.
A data frame with renamed columns
data_df <- readr::read_csv('
subject, age, gender, Aripiprazole, Clozapine, Olanzapine, Quetiapine
A, 27, F, 20, 10, 40, 25
B, 23, M, 21, 21, 35, 27
')
rename_with_seq(data_df, col_selector = Aripiprazole:Quetiapine, prefix = 'drug')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.