[R] Skipping lines and incomplete rows

Rui Barradas ruipbarradas at sapo.pt
Tue Jul 10 10:34:03 CEST 2012


Hello,

My approach was slightly different, to use readLines to take care of the 
header and read.table for the data. This works with the new dataset 
you've posted, but we must use the option comment.char = "".

Try the following.


head <- readLines("test.txt", n=4)[4]
dat <- read.table("test.txt", skip=5, sep=";", stringsAsFactors=FALSE, 
comment.char="c")
names(dat) <- unlist(strsplit(head, ";"))

dat$Time <- as.Date(dat$Time, format="%m/%d/%Y")
dat$Temp[dat$Temp == '#N/A'] <- NA
dat$Press[dat$Press == '#N/A'] <- NA
dat


It works with me, good luck.

Rui Barradas

Em 10-07-2012 06:41, vioravis escreveu:
> Thanks a lot Rui and Arun.
>
> The methods work fine with the data I gave but when I tried the two methods
> with the following semi-colon separated data using sep = ";". Only the first
> 3 columnns are read properly rest of the columns are either empty or NAs.
>
>
> **********************************************************************************************
> Remove this line
> Remove this line
> Remove this line
> Time;Actual Speed;Actual Direction;Temp;Press;Value1;Value2
> ;[m/s];[°];°C;[hPa];[MWh];[MWh]
> 1/1/2012;0.0;0;#N/A;#N/A;0.0000;0.0000
> 1/2/2012;0.0;0;#N/A;#N/A;0.0000;0.0000
> 1/3/2012;0.0;0;#N/A;#N/A;1.5651;2.2112
> 1/4/2012;0.0;0;#N/A;#N/A;1.0000;2.0000
> 1/5/2012;0.0;0;#N/A;#N/A;3.2578;7.5455
> ***********************************************************************************************
>
> I used the following code:
> dat1<-read.table("testInput.txt",sep=";",skip=3,fill=TRUE,header=TRUE)
> dat1<-dat1[-1,]
> row.names(dat1)<-1:nrow(dat1)
>
> Could you please let me know what is wrong with this approach?
>
> Thank you.
>
> Ravi
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Skipping-lines-and-incomplete-rows-tp4635830p4635952.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.
>



More information about the R-help mailing list