Annemarie Annemarie - 11 days ago 8
R Question

select different rows from grouped data

I have a dataset of repeated measurements over days for variables t, l and c. I am trying to calculate the difference in days between the peaks for t, and l/c. I have managed to find the day of peak measurement, but I am not sure how I can get these all on the same line so that I can calculate the day difference.

Record day t day_tmax l day_lmax c day_cmax
1 1 40.5 NA 2 NA 1.6 NA
1 2 136.8 2 2.5 2 190.6 NA
1 3 102.7 NA 1.6 NA 375.8 NA
1 4 55.1 NA 1.6 NA 515.5 4
1 5 32.3 NA 1.7 NA 411.2 NA
1 6 27.4 NA 1.5 NA 235.7 NA
1 7 17.7 NA 1.1 NA 187.1 NA
1 8 23 NA 1.4 NA 257 NA
1 9 17.2 NA 1.4 NA 170.5 NA
1 10 12.1 NA 0.6 NA 162.1 NA
2 1 72.2 NA 0.9 1 2.4 NA
2 2 54.3 NA 0.8 na 3.9 NA
2 3 64.4 NA 0.7 NA 4.6 3
2 4 63.1 NA 0.5 NA 3.1 NA
2 5 65.2 NA 0.3 NA 4.5 NA
2 6 66.5 NA 0.2 NA 4.6 6
2 7 81.5 7 0.2 NA 3.5 NA


I know in dplyr:

df1 <- df %>% group_by(record) %>% mutate(day_tmax = nth(day_tmax,2))


But is there a way to do this when the position of the original variable varies?

Answer

You could do:

df%>%
  group_by(Record)%>%
  mutate(diff_peak_t_lc=day[which.max(t)]-day[which.max(l/c)])

With day[which.max(t)] giving you the day for which t is at its peak and day[which.max(l/c)] giving you the day for which l/c is at its peak.

edit: Followed Paul Hiemstra's suggestion.