[R] Skipping lines and incomplete rows
arun
smartpink111 at yahoo.com
Thu Jul 12 00:35:00 CEST 2012
Hello,
Try this:
dat3<-read.table("dat3.txt",sep="",skip=3,header=TRUE,fill=TRUE)
dat4<-data.frame(variable1=paste(dat3[,1],dat3[,2],sep=" "),Variable2=dat3[,3],Variable3="",Variable4=dat3[,4])
dat4<-dat4[-1,]
row.names(dat4)<-1:nrow(dat4)
dat4
variable1 Variable2 Variable3 Variable4
1 11/1/2004 0:00 0.1 0.001
2 11/1/2004 0:10 0.2 0.002
3 11/1/2004 0:20 0.3 0.003
4 11/1/2004 0:30 0.4 0.004
#If you need to convert date to class "Date"
dat4$variable1<-as.Date(dat4[,1],format="%m/%d/%Y %H:%M")
A.K.
----- Original Message -----
From: vioravis <vioravis at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Wednesday, July 11, 2012 8:30 AM
Subject: Re: [R] Skipping lines and incomplete rows
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.
______________________________________________
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