[R] read table and import of a text file

arun smartpink111 at yahoo.com
Sat Oct 19 07:14:04 CEST 2013


Hi,

The code is based on what you pasted on the page. In the original post, you used sep="", while the pasted data shows "," as delimiter.
dat1 <- subset(head(read.table(text="#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",skip=6,sep=",",header=FALSE,fill=TRUE,stringsAsFactors=FALSE),-1),select=1:4)
 str(dat1)
#'data.frame':    21 obs. of  4 variables:
# $ V1: num  891231 900101 900101 900101 900101 ...
# $ V2: int  2400 100 200 300 400 500 600 700 800 900 ...
# $ V3: num  -1.5 -1.4 -1.6 -1.7 -2.1 -2.3 -2.4 -2.5 -2.6 -2.8 ...
# $ V4: num  -0.21 -0.25 -0.28 -0.25 -0.1 ...
dat1
       V1   V2   V3         V4
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
7  900101  600 -2.4 -0.2100000
8  900101  700 -2.5 -0.2800000
9  900101  800 -2.6 -0.3000000
10 900101  900 -2.8 -0.3000000
11 900101 1000 -2.8 -0.3000000
12 900101 1100 -2.7 -0.3000000
13 900101 1200 -3.0 -0.3000000
14 900101 1300 -3.2 -0.3000000
15 900101 1400 -3.5 -0.0999999
16 900101 1500 -4.0 -0.2000000
17 900101 1600 -4.5 -0.1900000
18 900101 1700 -5.3  0.2700000
19 900101 1800 -4.0 -0.2700000
20 900101 1900 -4.0 -0.2800000
21 900101 2000 -3.8 -0.2800000


A.K.








Thanks, but the code does not work.  I received an error like this one " Error in read.table(text = gsub("#|,$", "", temp[grepl(",", 
temp)][-1]),  : 
  duplicate 'row.names' are not allowed". 

In the sample data that I have pasted on this page the data has 
four columns and it starts at the following line "891231, 2400, -1.5, 
-0.21, ".  Anything before that is uninteresting for me.   


On Friday, October 18, 2013 10:46 AM, arun <smartpink111 at yahoo.com> wrote:
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