Jonathan
Jonathan

Reputation: 119

How to compute the mean for the last few rows in each time period in a data frame?

I have data collected for a few subjects, every 15 seconds over an hour split up by periods. Here's how the dataframe looks like, the time is "Temps", subjects are "Sujet" and the periods are determined by "Palier".

    data.frame':    2853 obs. of  22 variables:
 $ Temps         : Factor w/ 217 levels "00:15","00:30",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Sujet         : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Test          : Factor w/ 3 levels "VO2max","Tlim",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Palier        : int  1 1 1 1 1 1 1 1 1 1 ...
 $ RPE           : int  8 8 8 8 8 8 8 8 8 8 ...
 $ Rmec          : num  39.1 27.5 23.3 21.5 20.3 21.7 20.5 20.7 20.2 20.1 ...

Here a glimpse of the data.frame:

  Temps Sujet   Test Palier RPE Rmec Pmec Pchim Fr   Vt    VE  FeO2 FeCO2  VO2 VCO2  RER  HR VO2rel     VE.VO2    VE.VCO2
1 00:15     1 VO2max      1   8 39.1  185 473.6 19 1854 34.60 16.24  4.48 1353 1268 0.94 121   17.6 0.02557280 0.02728707
2 00:30     1 VO2max      1   8 27.5  185 672.4 17 2602 44.30 15.77  4.78 1921 1731 0.90 124   25.0 0.02306091 0.02559214
3 00:45     1 VO2max      1   8 23.3  185 794.5 18 2793 50.83 15.63  4.85 2270 2015 0.89 131   29.6 0.02239207 0.02522581
4 01:00     1 VO2max      1   8 21.5  185 860.3 20 2756 55.76 15.68  4.88 2458 2224 0.90 137   32.0 0.02268511 0.02507194
5 01:15     1 VO2max      1   8 20.3  185 909.3 23 2709 61.26 15.84  4.88 2598 2446 0.94 139   33.8 0.02357968 0.02504497
6 01:30     1 VO2max      1   8 21.7  185 853.7 21 2899 59.85 16.00  4.89 2439 2395 0.98 140   31.8 0.02453875 0.02498956

Each "Palier" lasts about 5 min and there are from 5 to 10 "Palier". For each subject and "Palier", I need to compute the mean for the last 2 min for all the variables. I haven't figured it out yet with dcast() or ddply(), but I am a newbie!

Any help would be much appreciated!

Upvotes: 1

Views: 195

Answers (1)

David Robinson
David Robinson

Reputation: 78600

If you turned it into a data.table (which you'd have to install), you could do this with

library(data.table)
dt = as.data.table(d)  # assuming your existing data frame was called d
last.two.min = dt[, mean(tail(Rmec, 9)), by=Sujet]

This assumes that your original data frame was called d, and that you want the last 9 items (since it is every 15 seconds- you might want the last 8 if you want from 58:15 to 60:00).

I assumed that Rmec was the variable you wanted to get the mean for. If there are multiple for which you want to get the mean, you can do something like:

last.two.min = dt[, list(mean.Rmec=mean(tail(Rmec, 9)),
                        mean.RPE=mean(tail(RPE, 9))), by=Sujet]

Upvotes: 2

Related Questions