2

I'm a complete newbie and am trying to change column names of a dataset in R. For example, to change the column name of 'Eth' in dataset quine to 'Ethnic'. Any help or the name of the function is greatly appreciated.

Nakx
  • 1,460
  • 1
  • 23
  • 32
CodeLearner
  • 389
  • 2
  • 6
  • 14

1 Answers1

5

The function 'colnames' is what you're looking for

let's say that 'Eth' is the third column, do

colnames(dataset)[3]<-"Ethnic"

colnames(dataset) returns exactly what you think it should, but you can also use it to set column names.

Doing the following

colnames(dataset)<-newColNames

where newColNames is a vector of names of the same length as the number of columns in dataset will change all the column names in order.

The following (as I did above)

colnames(dataset)[i]<-name

where name is a string and i is an integer, will change the name of the ith column to whatever the string "name" is

DMT
  • 1,577
  • 10
  • 16