Reputation: 3760
I need to calculate the time difference of the each sample (the samples can be differentiate using ID column) between two temperature points.
Here is the sample data:
> dput(data)
structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L), Zeit = c(0L, 180L, 360L, 420L, 600L, 604L, 0L, 180L,
360L, 480L, 600L, 605L), Temp = c(963L, 824L, 666L, 658L, 641L,
549L, 957L, 823L, 661L, 660L, 642L, 562L)), .Names = c("id",
"Zeit", "Temp"), row.names = c(NA, 12L), class = "data.frame")
Here is ggplot code:
ggplot(data, aes(x=Zeit,y=Temp,group=id, colour=factor(id))) + geom_line(size=2)
Here is an image with extra explanation:
I need to calculate the time (Zeit) that i need to reach the temp = 600°C (Temp) for each of the sample (ID). So in this example for the ID sample 1, to reach 600°C (Temp column) i need 600 seconds. I apprecieate any help!
Upvotes: 0
Views: 1233
Reputation: 1809
Use the approx
function. For a target temperature Tt, the time required is given by:
with(data, approx(x = Temp, y = Zeit, xout = Tt))$y
If the temperature rises with time at any point, then this may be more complicated - you will need to specify the ties
argument to approx
(probably put ties = min
in your context) - but it will work for your example as it is. Note that the values I use for x
and y
are inverted with respect to your axes.
To get the table that you desire:
Zt <- vapply(unique(data$id), function(ID){
with(data[data$id == ID,], approx(x = Temp, y = Zeit, xout = Tt))$y
}, double(1))
data.frame(id = unique(data$id), time = Zt)
Upvotes: 1