[R] read table and import of a text file

arun smartpink111 at yahoo.com
Fri Oct 18 16:46:42 CEST 2013


Hi,

Assuming that you provided the sample data from the file.
temp <- readLines(textConnection("#Hogd/met, Temp, 005[M], Value
#Hogd/met, Difftemp, 051[M], Value
BA0+
1 MTEMP005 1 [deg.C]
2 MDTMP051 1 [deg.C]
EOH
891231, 2400, -1.5, -0.21,
900101, 0100, -1.4, -0.25,
900101, 0200, -1.6, -0.28,
900101, 0300, -1.7, -0.25,
900101, 0400, -2.1, -0.0999999,
900101, 0500, -2.3, -0.0899999,
900101, 0600, -2.4, -0.21,
900101, 0700, -2.5, -0.28,
900101, 0800, -2.6, -0.3,
900101, 0900, -2.8, -0.3,
900101, 1000, -2.8, -0.3,
900101, 1100, -2.7, -0.3,
900101, 1200, -3, -0.3,
900101, 1300, -3.2, -0.3,
900101, 1400, -3.5, -0.0999999,
900101, 1500, -4, -0.2,
900101, 1600, -4.5, -0.19,
900101, 1700, -5.3, 0.27,
900101, 1800, -4, -0.27,
900101, 1900, -4, -0.28,
900101, 2000, -3.8, -0.28,
EOF")) 

temp1 <- read.table(text=gsub("#|,$","",temp[grepl(",",temp)][-1]),sep=",",header=TRUE,check.names=FALSE)
 head(temp1)
#  Hogd/met Difftemp 051[M]      Value
#1   891231     2400   -1.5 -0.2100000
#2   900101      100   -1.4 -0.2500000
#3   900101      200   -1.6 -0.2800000
#4   900101      300   -1.7 -0.2500000
#5   900101      400   -2.1 -0.0999999
#6   900101      500   -2.3 -0.0899999


A.K.



I have a text file which was imported imperfectly.  I used the following code: 
temp<-read.table("/New/temp.txt",skip=6,header = TRUE, sep="") 

However the result is not what I expected and looks like: 

> head(temp) 
       X891231..2400...1.5...0.21. 
1      900101, 0100, -1.4, -0.25, 
2      900101, 0200, -1.6, -0.28, 
3      900101, 0300, -1.7, -0.25, 
4 900101, 0400, -2.1, -0.0999999, 
5 900101, 0500, -2.3, -0.0899999, 
6      900101, 0600, -2.4, -0.21, 

Sample data with header and footer is found here: 

#Hogd/met, Temp, 005[M], Value 
#Hogd/met, Difftemp, 051[M], Value 
BA0+ 
1 MTEMP005 1 [deg.C] 
2 MDTMP051 1 [deg.C] 
EOH 
891231, 2400, -1.5, -0.21, 
900101, 0100, -1.4, -0.25, 
900101, 0200, -1.6, -0.28, 
900101, 0300, -1.7, -0.25, 
900101, 0400, -2.1, -0.0999999, 
900101, 0500, -2.3, -0.0899999, 
900101, 0600, -2.4, -0.21, 
900101, 0700, -2.5, -0.28, 
900101, 0800, -2.6, -0.3, 
900101, 0900, -2.8, -0.3, 
900101, 1000, -2.8, -0.3, 
900101, 1100, -2.7, -0.3, 
900101, 1200, -3, -0.3, 
900101, 1300, -3.2, -0.3, 
900101, 1400, -3.5, -0.0999999, 
900101, 1500, -4, -0.2, 
900101, 1600, -4.5, -0.19, 
900101, 1700, -5.3, 0.27, 
900101, 1800, -4, -0.27, 
900101, 1900, -4, -0.28, 
900101, 2000, -3.8, -0.28, 
EOF 

I have a number of similar files and would like to understand 
what I did wrong. I also wish to understand the anatomy of this text 
file. What does EOH mean? and EOF? I could not find this issues on web 
search. Thanks



More information about the R-help mailing list