Skip to content
Prev 68814 / 398506 Next

How to change variables in datasets automatically

On 4/29/05, Muhammad Subianto <subianto at gmail.com> wrote:
Someone else has already indicated how to do this but as you say you
have a large number of columns you might want an automated way as
well.  For example the following removes lower case letters and dots
from the names and then changes Species to Class.   Note that there is
a dot after a-z

# remove lower case letters and dots from column names and 
# change name of col5 to Class
data(iris)
names(iris) <- gsub("[a-z.]", "", names(iris)) 
names(iris)[5] <- "Class"

Another possibility might be to use abbreviate.  This does
not give the exact result you are looking for but its close
and its very easy:

data(iris)
names(iris) <- abbreviate(names(iris))
names(iris)[5] <- "Class"