R: Calculate measurement time-points for separate samples

孤街醉人 提交于 2019-12-11 17:43:38

问题


I have measured concentrations of N2O for different samples (Series) during 10 min intervals. Each sample was measured two times a day for 9 days. The N2O analyzer saved data (conc.) every second!

My data now looks like this:

                   DATE Series        V       A         TIME Concentration
1: 2017-10-18T00:00:00Z    O11 0.004022 0.02011 10:16:00.746     0.3512232
2: 2017-10-18T00:00:00Z    O11 0.004022 0.02011 10:16:01.382     0.3498687
3: 2017-10-18T00:00:00Z    O11 0.004022 0.02011 10:16:02.124     0.3482681
4: 2017-10-18T00:00:00Z    O11 0.004022 0.02011 10:16:03.216     0.3459306
5: 2017-10-18T00:00:00Z    O11 0.004022 0.02011 10:16:04.009     0.3459124
6: 2017-10-18T00:00:00Z    O11 0.004022 0.02011 10:16:04.326     0.3456660

I would like to analyze gas fluxes using the R HMR package. For this, I need to calculate measurement time-points in increasing order out of the exact time (TIME) data points. The time should look like this (table taken from https://cran.r-project.org/web/packages/HMR/HMR.pdf)

Series;V;A;Time;Concentration
k0a; 140.6250; 0.5625; 0; 13.98
k0a; 140.6250; 0.5625; 10; 14.65
k0a; 140.6250; 0.5625; 20; 15.15
k0a; 140.6250; 0.5625; 30; 15.85

How can I calculate this for each individual 10-minute measurement period for each pot? Basically it should list the increasing nr. of seconds as my machine measured conc. every second.

My idea is to group by "Series" and "DATE" and do a loop. Inspired by R: calculate time difference between specific events Something like:

library(dplyr)
df.HMR %>% group_by(DATE, Series) %>% 
  mutate(time_diff = ????)

I would be very grateful for your help!


回答1:


Use lag may solve the problem.

df.HMR=read.table(text="No DATE Series V A TIME Concentration 
              1: 2017-10-18T00:00:00Z O11 0.004022 0.02011 10:16:00.746 0.3512232 
              2: 2017-10-18T00:00:00Z O11 0.004022 0.02011 10:16:01.382 0.3498687 
              3: 2017-10-18T00:00:00Z O11 0.004022 0.02011 10:16:02.124 0.3482681 
              4: 2017-10-18T00:00:00Z O11 0.004022 0.02011 10:16:03.216 0.3459306 
              5: 2017-10-18T00:00:00Z O11 0.004022 0.02011 10:16:04.009 0.3459124 
              6: 2017-10-18T00:00:00Z O11 0.004022 0.02011 10:16:04.326 0.3456660",
                  header=T,stringsAsFactors=FALSE)

df.HMR %>% group_by(DATE, Series) %>% 
  mutate(dt=as.POSIXct(df.HMR$TIME,format="%H:%M:%S"), time_diff = dt-lag(dt))


来源:https://stackoverflow.com/questions/52839735/r-calculate-measurement-time-points-for-separate-samples

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!