[R] problems with reading the data and merge

Daniel Folkinshteyn dfolkins at gmail.com
Fri Jun 6 06:17:59 CEST 2008


the '00' entries may be in a numeric column, so it gets typecast to a 
number, and of course 00 == 0, numerically speaking, so they get 
'condensed'.

to be sure you read everything "as is", specify "colClasses='character'. :
data<-read.table("data.txt",sep='\t', header=T, colClasses='character')
will read all data in character form, and no truncation of initial zeros 
will take place in even all-numeric columns.

when you read in with "header=F", what happens is that column names 
(which are probably letters rather than numbers), become part of the 
column, and then the column is not all numeric, and is thus typecast to 
a factor, rather than to numeric, and thus the 00's are preserved.

hope that clarifies things. (and you can always see ?read.table for more 
detailed explanations)

on 06/05/2008 07:58 PM kayj said the following:
> I have a problem with reading a file data.txt that has a header 
> Each row has the individual ID and then some data  that are letter and
> numbers such as 00
> If I read the file as 
> 
>   data<-read.table("data.txt",sep='\t', header=T)
>   write.table(data,file="data1.txt", sep='\t', quote=F, col.names=T,
> row.names=F)
> 
> data1.txt looks different than data.txt. In data1.txt some of the 00 entries
> are replaced with just a 0!
> 
> But If I read the file as 
> data<-read.table("data.txt",sep='\t', header=F)
> write.table(data,file="data2.txt", sep='\t', quote=F, col.names=F,
> row.names=F)
> 
> Then data2.txt looks exactly like data.txt (no problem)
> 
> My problem is when I need to merge data with another data set for example
> data3,
> How can I telll R to merge them If I  specify the header=F, 
> I tried 
> 
> M<-merge(data, data3, by”V1”)  This does not work.
> 
> What is the problem? I hope someone can help
> 
> 



More information about the R-help mailing list