Thom_G
Thom_G

Reputation: 29

How to rename every variable of a data set?

I have two dataset and I want to add _pre to the name of every variable of the first data set and _post to the name of every variable in the second data set.

So variables in the first data set would be named like var1_pre var2_pre and variables in the second set would be named var1_post var2_post...

Thanks.

Upvotes: 0

Views: 82

Answers (1)

Jan
Jan

Reputation: 5254

You can access the names of a data frame with names(). With paste (or paste0in this case) you can concatenate the names and "_pre" / "_post".

df <- data.frame(a = 1:10, b = 10:1)
names(df) <- paste0(names(df), "_pre")

Upvotes: 1

Related Questions