Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use read.table and rename column names

Tags:

r

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,

like image 677
user3576898 Avatar asked Dec 03 '22 19:12

user3576898


2 Answers

read.table has a col.names argument

read.table("/path/to/file", header=TRUE, col.names=c("PatID", "Age", "Sex", "Weight"))
like image 194
GSee Avatar answered Dec 24 '22 14:12

GSee


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.

like image 37
user3569035 Avatar answered Dec 24 '22 13:12

user3569035