[R] Skipping lines and incomplete rows

vioravis vioravis at gmail.com
Wed Jul 11 14:30:11 CEST 2012


Thanks a lot for the guidance. I have another text file with a time stamp and
an empty column as given below:

********************************************************************************************
First line: Skip this line 
Second line: skip this line 
Third line: skip this line 
variable1 Variable2 Variable3 Variable4 
                Unit1     Unit2     Unit3 
11/1/2004 0:00  0.1                 0.001 
11/1/2004 0:10  0.2                 0.002 
11/1/2004 0:20  0.3                 0.003 
11/1/2004 0:30  0.4                 0.004 
********************************************************************************************

This is space separated text file. When I use the following code:

head <- readLines("testInput.txt", n=4)[4] 
dat <- read.table("testInput.txt", skip=5, sep="",fill = TRUE,
stringsAsFactors=FALSE) 
names(dat) <- unlist(strsplit(head, " "))

I get the following output:

> str(dat)
'data.frame':   4 obs. of  4 variables:
 $ variable1: chr  "11/1/2004" "11/1/2004" "11/1/2004" "11/1/2004"
 $ Variable2: chr  "0:00" "0:10" "0:20" "0:30"
 $ Variable3: num  0.1 0.2 0.3 0.4
 $ Variable4: num  0.001 0.002 0.003 0.004

Variable1's date and time gets split as Variable1 and Variable2 whereas they
should both be part of Variable1.

Also, the empty column is missing from the data frame.

Is there a way to handle these two cases? 

Thank you.

Ravi


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



More information about the R-help mailing list