Skip to content

Help with parsing a data file

3 messages · Sean, Peter Alspach, Henrique Dallazuanna

#
Sean

I'm sure there are many ways of doing this.  I assume you have read the
data in R as a data.frame with 24 columns and 2+1+13+(1+13)*n rows,
where n is the number of years, and that you want a data.frame with 25
columns (one extra for year) and 13*n rows (although I am not sure why
13 MOnths) and the columns named appropriately.

#First create your new data.frame
newDF <- as.data.frame(matrix(NA, (nrow(oldDF)-15)/14, 25,
                       dimnames=list(NULL, c('year', oldDF[3,]))))
#Now fill the year column (column 1)
newDF[,1] <- rep(oldDf[seq(16, nrow(oldDF), 14),1], each=13)
#And finally deal with the data
newDF[,-1] <- oldDF[-(1:15),][c(F, rep(T,13)),]

The above is untested and not guaranteed to work, but hopefully is
enough to get you going.  If not, you could get back to me privately.

Peter Alspach
The contents of this e-mail are privileged and/or confidential to the named
 recipient and are not to be used by any other person and/or organisation.
 If you have received this e-mail in error, please notify the sender and delete
 all material pertaining to this e-mail.
#
Try this:

lines <- readLines('yourfile')
newLines <- lines[-(1:(13+3))]
coln <-  scan(textConnection(lines[3]), what="")
lapply(which(nchar(newLines) == 4),
function(x)read.table(textConnection(newLines[seq(x + 1, x + 13)]),
col.names=coln))
On 06/03/2008, sean <smachin1000 at gmail.com> wrote: