Skip to content
Prev 149419 / 398498 Next

Parsing

This should do what you want: (it uses loops; you can work at
replacing those with 'lapply' and such -- it all depends on if it is
going to take you more time to rewrite the code than to process a set
of data; you never did say how large the data was).  This also "grows"
a data.frame, but you have not indicated how efficient is has to be.
So this could be used as a model.
+ y      y_string
+ id1    id1_string
+ id2    id2_string
+ z      z_string
+ w      w_string
+ stuff  stuff  stuff
+ stuff  stuff  stuff
+ stuff  stuff  stuff
+ //
+ x      x_string1
+ y      y_string1
+ z      z_string1
+ w      w_string1
+ stuff  stuff  stuff
+ stuff  stuff  stuff
+ stuff  stuff  stuff
+ //
+ x      x_string2
+ y      y_string2
+ id1    id1_string1
+ id2    id2_string1
+ z      z_string2
+ w      w_string2
+ stuff  stuff  stuff
+ stuff  stuff  stuff
+ stuff  stuff  stuff
+ //"))
+     if (x[i] == "//"){  # output the current data
+         .save <- rbind(.save, .out)
+         .out <- .keys    # setup for the next pass
+     } else {
+         .split <- strsplit(x[i], "\\s+")
+         if (.split[[1]][1] %in% names(.out)){
+             .out[[.split[[1]][1]]] <- .split[[1]][2]
+         }
+     }
+ }
x         y         id1         id2         w
1  x_string  y_string  id1_string  id2_string  w_string
2 x_string1 y_string1        <NA>        <NA> w_string1
3 x_string2 y_string2 id1_string1 id2_string1 w_string2
On Wed, Jul 9, 2008 at 5:33 AM, Paolo Sonego <paolo.sonego at gmail.com> wrote: