Skip to content
Prev 299514 / 398503 Next

Skipping lines and incomplete rows

Hi,

I guess you should have "fill=TRUE" in the read.table.

dat1<-read.table(text="
First line: Skip this line
Second line: skip this line
Third line: skip this line
variable1 Variable2 Variable3 Variable4
????????????????? Unit1??????? Unit2??????? Unit3
10????????????? 0.1????????????? 0.01????????? 0.001
20????????????? 0.2????????????? 0.02????????? 0.002
30????????????? 0.3????????????? 0.03????????? 0.003
40????????????? 0.4????????????? 0.04????????? 0.004
",sep="",skip=4, fill=TRUE,header=TRUE)
dat1<-dat1[-1,]
row.names(dat1)<-1:nrow(dat1)
dat1
? variable1 Variable2 Variable3 Variable4
1??????? 10?????? 0.1????? 0.01???? 0.001
2??????? 20?????? 0.2????? 0.02???? 0.002
3??????? 30?????? 0.3????? 0.03???? 0.003
4??????? 40?????? 0.4????? 0.04???? 0.004


A.K.




----- Original Message -----
From: vioravis <vioravis at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, July 9, 2012 6:23 AM
Subject: [R] Skipping lines and incomplete rows

I have a text file that has semi-colon separated values. The table is nearly
10,000 by 585. The files looks as follows:

*******************************************
First line: Skip this line
Second line: skip this line
Third line: skip this line
variable1 Variable2 Variable3 Variable4
? ? ? ? ? ? ? ? ?  Unit1? ? ? ?  Unit2? ? ? ?  Unit3
10? ? ? ? ? ? ? 0.1? ? ? ? ? ? ?  0.01? ? ? ? ?  0.001
20? ? ? ? ? ? ? 0.2? ? ? ? ? ? ?  0.02? ? ? ? ?  0.002 
30? ? ? ? ? ? ? 0.3? ? ? ? ? ? ?  0.03? ? ? ? ?  0.003
40? ? ? ? ? ? ? 0.4? ? ? ? ? ? ?  0.04? ? ? ? ?  0.004
*******************************************

The first three lines need to be skipped. Moreover, line 5 doesn't have
units for all the variables and hence, has to be skipped as well.
Effectively, I want the following to be read to a dataframe skipping rows 1,
2, 3 and 5.

*******************************************
variable1 Variable2 Variable3 Variable4
10? ? ? ? ? ? ? 0.1? ? ? ? ? ? ?  0.01? ? ? ? ?  0.001
20? ? ? ? ? ? ? 0.2? ? ? ? ? ? ?  0.02? ? ? ? ?  0.002 
30? ? ? ? ? ? ? 0.3? ? ? ? ? ? ?  0.03? ? ? ? ?  0.003
40? ? ? ? ? ? ? 0.4? ? ? ? ? ? ?  0.04? ? ? ? ?  0.004
*******************************************

I tried using read.table with skip for line 1-3 as follows 

inputData <- read.table("test.txt",sep = ";",skip = 3)

but the line 4 is creating problem with the following error:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, 
: 
? line 3 did not have 585 elements

Can someone help me with this?

Thank you.

Ravi

--
View this message in context: http://r.789695.n4.nabble.com/Skipping-lines-and-incomplete-rows-tp4635830.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.