MirrG
MirrG

Reputation: 406

Assign new column names if a dataframe's name is called as a string variable

I have a data frame, the name of which is wrapped in a variable "my_var". What I am trying to do is to assign a new column names for this data.

What I have tried is:

names(get(my_var)) <- c("Fruits", "Sweets")  
assign(names(get(my_var)),  c("Fruits", "Sweets"))
within (get(my_var), assign(names(get(my_var)),  c("Fruits", "Sweets")))

Neither of this works.

Thanks

Upvotes: 0

Views: 141

Answers (1)

Metariat
Metariat

Reputation: 532

you can use setnames from the data.table package:

setnames(get(my_var), names(get(my_var)), c('a', 'b'))

Upvotes: 1

Related Questions