Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

r use make.names() to rename columns

Writer Matthew Barrera

i'm using the make.names() column to create better column names, removing illegal symbols etc. how do I apply the new column names to the dataset?

this produces the vector of new column names:

names(data) %>% make.names()

i've tried these approaches to get the new column names to replace the old ones (these don't work the way I want):

names(data) %>% make.names() <- data
names(data) %>% make.names() <- names(data)
data <- names(data) %>% make.names()

2 Answers

You almost had it:

names(data) <- names(data) %>% make.names()

With dplyr, we can do

library(dplyr)
data <- data %>% set_names(make.names(names(.)))

Or with rename_all

data <- data %>% rename_all(~ make.names(.))
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.