my_rename_fn | R Documentation |
Renaming columns is easy enough, but if you want to rename a bunch of them and you don't have a clear pattern, it can be kind of a pain. This function allows you to take an existing data frame and rename using a separate spreadsheet of names.
my_rename_fn(x, old_names, new_names)
x |
The dataframe or tibble whose names you want to change. |
old_names |
A vector of any names you want to change. |
new_names |
A vector of the new names you want |
The most common usage of this would be to import an excel sheet in which you have manually matched old_names
and new_names
.
Thus it is important that the two vectors are of the same length and that each value in old_names
matches up with the corresponding value
in new_names
.
x, renamed as requested.
x <- tibble(a = c(1,2,3), b = c(2,3,4), c = c(3,4,5)) r <- tibble(old_names = c("a","b","d"), new_names = c("a.new", "b.new", "d.new")) my_rename_fn(x, r$old_names, r$new_names)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.