[R] Extraction and replacement of data in a data frame

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jan 18 03:31:16 CET 2011


On Mon, Jan 17, 2011 at 3:51 PM, michael.hopgood <michael.hopgood at mrm.se> wrote:
>
> Dear R family,
> I am a relative newbie and have been dabbling with R for a little while.
> Simple things really, but my employers are beginning to see the benefits of
> using R instead of excel. We have a remote monitoring station measuring
> groundwater levels.  We download the  date as a .csv file and up until now,
> we have been using excel to analyse the data.  It’s been a hassle trying to
> wrestle with that damn program as my boss wants to do things that excel was
> never meant to do,  so I’ve convinced my boss to give R a chance.  It’s been
> a steep learning curve, but I’m fairly confident I can reduce the amount of
> labour involved in producing and improving the graphs we show our clients.
>
> The groundwater levels are measured by pressure sensors lowered into the
> monitoring wells.   After a certain time, the sensors were lowered further
> into the well, thus creating a disparity in the measurements.
>
> The data frame I import into R looks something like this:
> Date            Waterhead (mm)
> 10-01-01     100
> 10-01-02     105
> 10-01-03     101
> 10-01-04      99
> 10-01-05      85
> 10-01-06    200
> 10-01-07    199
> 10-01-08    195
> 10-01-09    185
> 10-01-10    170
>
> For example, on the 10-10-06, the sensor was lowered by 115 mm.
> When I download the csv file, I download the data from the beginning of the
> measurement period. I then need to adjust the height by 115 mm to account
> for the lowering of the parameter.  My question to you is how do I do that
> in R?
> I am after a formula or a manipulation that selects the first five
> measurements and adds a fixed amount.  This is something that is added
> everytime I download the csv file and import it into R so that when I
> display my data, it is based on the following data frame:
>
> Date            Waterhead (mm)
> 10-01-01     215
> 10-01-02     220
> 10-01-03     216
> 10-01-04      214
> 10-01-05      200
> 10-01-06    200
> 10-01-07    199
> 10-01-08    195
> 10-01-09    185
> 10-01-10    170
>
> In short, I want to select a fixed number of rows of a column from my data
> frame, add a constant to these, and insert the new values into their
> respective rows without affecting the subsequent rows.  I hope I have
> produced a reproducible example.  I have been searching high and low for a
> solution, but have come up against a brick wall. I feel I have read
> something that tackles this some time in the past, but can’t find it again.
> Thanks in advance!

Try this using the builtin data frame, BOD:

> BOD
  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8
>
> # add 100 to the first two rows in column 2
> BOD[1:2, 2] <- BOD[1:2, 2] + 100
> BOD
  Time demand
1    1  108.3
2    2  110.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list