[Rd] Scan data from a .txt file

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 17 17:13:36 CET 2005


On 11/17/2005 10:35 AM, Vasundhara Akkineni wrote:
> Hi all,
> Am trying to read data from a .txt file in such a way that i can access the
> column names too. For example, the data in the table.txt file is as below:
>  Name Weight Height Gender
> Anne 150 65 F
> Rob 160 68 M
> George 180 65 M
> Greg 205 69 M
>  i used the following commands:
>  data<-scan("table.txt",list("",0,0,0),sep="")
> a<-data[[1]]
> b<-data[[2]]
> c<-data[[3]]
> d<-data[[4]]
>  But this doesn't work because of type mismatch. I want to pull the col
> names also into the respective lists. For example i want 'b' to have
> (weight,150,160,180,205) so that i can access the col name and also the
> induvidual weights. I tried using the read.table method too, but couldn't
> get this working. Can someone suggest a way to do this.

You probably want to use read.table("table.txt", head=TRUE).  Chances 
are it will automatically recognize the column types; if not, there's 
the colClasses argument.

Duncan Murdoch



More information about the R-devel mailing list