如何计算浓度多次超过阈值的总持续时间? [英] How to calculate the total time duration of concentration crossing a threshold multiple times?

查看:36
本文介绍了如何计算浓度多次超过阈值的总持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 R,我试图计算每个人的总持续时间,其中这个持续时间是花费在特定阈值之上的时间.

例如,在下面的图中,我有 3 个受试者 (ID) 的浓度数据,我想找到每个人在蓝色虚线上方花费的时间(x 轴).数据集结构类似于:

head(数据集)ID时间CP1 1 0.0 0.000000002 1 0.0 0.000000003 1 0.5 0.037598064 1 1.0 0.125234555 1 1.5 0.234832196 1 2.0 0.34820905

我如何编写一个代码,通过排除它们来考虑浓度下降到虚线以下的时间.最终结果将是蓝色虚线上方所有时间的总持续时间.如下图

解决方案

所以,感谢 rhole 提供如何解决问题的想法.下面的代码帮助我进行了分析,但是我必须添加一个名为Day"的变量,然后计算每天的持续时间.在这里我使用了 day 因为每天有一个间隔.但您可以根据需要进行调整.

#sub-setting 按天dataset$Day[dataset$time>=0 &dataset$time<24] <-第 1 天"dataset$Day[dataset$time>=24 &dataset$time<48] <-第 2 天"dataset$Day[dataset$time>=48 &dataset$time<72] <-第 3 天"#每天#TAbove<-setDT(dataset)[CP>.05, diff((时间)), by = .(ID,Day)]图书馆(plyr)# 总结每人每天的持续时间sumPerDay<-summarise(group_by(TAbove, ID,Day),总和=总和(V1))# 总结每人所有天的持续时间sumAll<-summarise(group_by(TAbove, ID),总和=总和(V1))

Using R, I am trying to calculate the total time duration for each individual where this time duration is the time spent above certain threshold.

For example, in the plot below I have the concentration data for 3 subjects (ID), and I would like to find the time (x axis) spent above the blue dashed line for each individual. the data set structure would be something like:

head(dataset)
  ID time      CP
1  1  0.0 0.00000000
2  1  0.0 0.00000000
3  1  0.5 0.03759806
4  1  1.0 0.12523455
5  1  1.5 0.23483219
6  1  2.0 0.34820905

Solid lines represent the concentrations for 3 different subjects

I tried to use the following code:

library(data.table) 
TAbove<-setDT(dataset)[CP > .05, diff(range(time)), by = ID]

However, this code that it calculates the time duration from first rise above dashed blue line to the last drop. For example for the green line ID, see the black line.

How can I write a code that takes into account the times where the concentrations drop below the dashed line, by excluding them. the final result would be a total time duration of all the times above the dashed blue line. like below

解决方案

So, thanks to rhole for providing the idea of how to solve the question. the code below helped me do the analysis, however I had to add a variable called "Day", and then calculate the time duration per day. Here I used day because there is one interval per day. But you can adjust it according to your need.

#sub-setting by day
dataset$Day[dataset$time>=0 &dataset$time<24] <- "Day 1"
dataset$Day[dataset$time>=24 &dataset$time<48] <- "Day 2"
dataset$Day[dataset$time>=48 &dataset$time<72] <- "Day 3"
#per day#
TAbove<-setDT(dataset)[CP > .05, diff((time)), by = .(ID,Day)]
library(plyr)
# sum the time duration for each day per person
sumPerDay<-summarise(group_by(TAbove, ID,Day),
           sum=sum(V1))
# sum the time duration for ALL days per person
sumAll<-summarise(group_by(TAbove, ID),
                 sum=sum(V1))

这篇关于如何计算浓度多次超过阈值的总持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆