user3576898
user3576898

Reputation: 65

Use read.table and rename column names

I am new to r and am using the read.table command to import a text file. My column names are PatID, AgePat, SexPat and WeightPat. I want to change these to simply PatID (no change), Age, Sex and Weight. How do I do this? Thanks,

Upvotes: 6

Views: 29864

Answers (3)

user3569035
user3569035

Reputation: 234

Here are two ways to do what you want:

colnames(data) [2:4] <- c("Age","Sex","Income")

or

colnames(x)[2:4] <- sub("Pat","",colnames(x)[2:4])

If you're new to R, I would recommend the ebook "R Fundamentals & Graphics" which will give you a solid grasp of R basics. Better than fumbling around and wasting your time.

Upvotes: 4

GSee
GSee

Reputation: 49810

read.table has a col.names argument

read.table("/path/to/file", header=TRUE, col.names=c("PatID", "Age", "Sex", "Weight"))

Upvotes: 13

sriramn
sriramn

Reputation: 2378

Say the DataFrame name is Data. Just do:

names(Data) <- c("PatID", "Age", "Sex", "Weight")

Upvotes: 1

Related Questions