[R] changing colnames

Daniel Nordlund djnordlund at frontier.com
Wed Aug 14 21:31:29 CEST 2013


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of arun
> Sent: Wednesday, August 14, 2013 11:56 AM
> To: farnoosh sheikhi
> Cc: R help
> Subject: Re: [R] changing colnames
> 
> Hi,
> You could try:
> dat1<- read.table(text="
> X1,X2,X3
> age,race,stat
> 12,2,35
> 17,6,55
> ",sep=",",header=TRUE,stringsAsFactors=FALSE)
> colnames(dat1)<- dat1[1,]
>  dat1<- dat1[-1,]
> dat1[]<-lapply(dat1,as.numeric)
> row.names(dat1)<- 1:nrow(dat1)
>  dat1
> #  age race stat
> #1  12    2   35
> #2  17    6   55
> A.K.
> 

Well, your data does not match up with what arun used.  You have space delimited variable names and no spaces between  any of the numbers.  If the number should be broken up like arun has comma separated, then it is even easier.  I stored the following data in a file, temp.txt,

X1 X2 X3
age race stat
12 2 35
17 6 55

and read it like this


dat1<- read.table("c:/tmp/temp.txt", header=TRUE, skip=1)

and got this 

> dat1
  age race stat
1  12    2   35
2  17    6   55
>


Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA
 
> 
> 
> 
> 
> 
> ________________________________
> From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
> To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
> Sent: Wednesday, August 14, 2013 2:45 PM
> Subject: changing colnames
> 
> 
> 
> Hi Arun,
> 
> I have a data set as follow:
> 
> X1 X2 X3
> age race stat
> 12235
> 17655
> 
> The column names are currently  X1 X2 X3, but I want to drop them and have
> age, race and stat as column names, like below:
> 
> age race stat
> 12235
> 17655
> 
> Do you know how to do that?
> Thanks a lot.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list