[R] Gobbling up a repeating, irregular list of data

Morway, Eric emorway at usgs.gov
Fri Nov 11 05:26:38 CET 2016


What would be the sophisticated R method for reading the data shown below
into a list?  The data is output from a numerical model.  Pasting the
second block of example R commands (at the end of the message) results in a
failure ("Error in scan...line 2 did not have 6 elements").  I no doubt
could cobble together some script for reading line-by-line using for loops,
and then appending vectors with values from each line, but this strikes me
as bad form.

One final note, the lines with 6 values contain important values that
should somehow remain associated with the data appearing in columns 5 & 6
(the continuous data).  The first value, which is always 1, can be
discarded, but the second value on these lines contain the time step number
("1.00E+00", "2.00E+00", etc.), the 3rd and 4th values are contain a depth
and thickness, respectively. Columns 5 & 6 are a depth and water content
pairing and should be associated with the time steps.

Thanks, Eric

Start of example output data (Use of an R script to read in this data below)

  1    1.00E+00  1.24E+03  7.79E+00  1.925E-01  1.88E-01
                                     3.850E-01  1.88E-01
                                     5.775E-01  1.88E-01
                                     7.700E-01  1.88E-01
                                     9.626E-01  1.88E-01
                                     1.155E+00  1.88E-01
                                     1.347E+00  1.88E-01
  1    2.00E+00  1.26E+03  7.80E+00  1.925E-01  2.80E-01
                                     1.732E+00  2.80E-01
                                     1.925E+00  2.80E-01
                                     2.310E+00  2.93E-01
                                     2.502E+00  2.22E-01
                                     2.695E+00  1.88E-01
                                     2.887E+00  1.88E-01
  1    3.00E+00  1.28E+03  7.70E+00  1.925E-01  1.03E-01
                                     3.850E-01  1.30E-01
                                     5.775E-01  1.48E-01
                                     7.701E-01  1.61E-01
                                     9.626E-01  1.72E-01
                                     1.155E+00  1.86E-01
                                     1.347E+00  1.93E-01
  1    4.00E+00  1.29E+03  7.60E+00  1.901E-01  1.80E-01
                                     3.803E-01  1.80E-01
                                     5.705E-01  1.38E-01
                                     7.607E-01  1.32E-01
                                     2.282E+00  1.86E-01
                                     2.472E+00  1.98E-01
                                     2.662E+00  2.00E-01

Same data as above, but scan function fails.

dat <- read.table(textConnection("  1    1.00E+00  1.24E+03  7.79E+00
 1.925E-01  1.88E-01
                                     3.850E-01  1.88E-01
                                     5.775E-01  1.88E-01
                                     7.700E-01  1.88E-01
                                     9.626E-01  1.88E-01
                                     1.155E+00  1.88E-01
                                     1.347E+00  1.88E-01
  1    2.00E+00  1.26E+03  7.80E+00  1.925E-01  2.80E-01
                                     1.732E+00  2.80E-01
                                     1.925E+00  2.80E-01
                                     2.310E+00  2.93E-01
                                     2.502E+00  2.22E-01
                                     2.695E+00  1.88E-01
                                     2.887E+00  1.88E-01
  1    3.00E+00  1.28E+03  7.70E+00  1.925E-01  1.03E-01
                                     3.850E-01  1.30E-01
                                     5.775E-01  1.48E-01
                                     7.701E-01  1.61E-01
                                     9.626E-01  1.72E-01
                                     1.155E+00  1.86E-01
                                     1.347E+00  1.93E-01
  1    4.00E+00  1.29E+03  7.60E+00  1.901E-01  1.80E-01
                                     3.803E-01  1.80E-01
                                     5.705E-01  1.38E-01
                                     7.607E-01  1.32E-01
                                     2.282E+00  1.86E-01
                                     2.472E+00  1.98E-01
                                     2.662E+00  2.00E-01"),header=FALSE)

	[[alternative HTML version deleted]]



More information about the R-help mailing list