[R] Script help: Determining Time Difference between two data points.
APOCooter
mikeedinger16 at gmail.com
Tue Jul 17 14:53:46 CEST 2012
I have the following data (a subset of the real data set):
structure(list(Date = structure(c(1209625080, 1209641460, 1209652500,
1209676800, 1209682860, 1209692100, 1209706980, 1209722580, 1209726300,
1209739620, 1209762780, 1209765720, 1209770520, 1209791040, 1209812580,
1209829920, 1209837180, 1209848160, 1209854640, 1209859440, 1209870780,
1209887760, 1209901080, 1209921660, 1209929280, 1209945600, 1209957240,
1209980280, 1210001760, 1210017000, 1210021140, 1210034820, 1210042800,
1210048980, 1210061520, 1210074480, 1210081200, 1210089300, 1210095960,
1210104120, 1210110900, 1210110900, 1210118400, 1210126980, 1210134180,
1210142640, 1210156080, 1210164180, 1210176840, 1210183740, 1210196820,
1210202400, 1210217700, 1210229280, 1210238280, 1210259520, 1210274100,
1210283100, 1210291560, 1210291620, 1210309260, 1210333440, 1210349520,
1210360440, 1210376580, 1210382520, 1210389960, 1210398240, 1210406580,
1210413240, 1210439220, 1210444800, 1210453140, 1210461900, 1210467060,
1210475160, 1210488900, 1210500000, 1210504980, 1210512780, 1210523820,
1210530900, 1210544340, 1210555800, 1210563120, 1210564980, 1210572600,
1210585620, 1210595520, 1210602840, 1210613820, 1210618200, 1210633260,
1210640940, 1210650240, 1210663560, 1210677660, 1210686960, 1210695240,
1210705380), class = c("POSIXct", "POSIXt"), tzone = ""), Score = c(80L,
11L, 81L, 152L, 130L, 122L, 142L, 20L, 1L, 31L, 93L, 136L, 128L,
112L, 48L, 57L, 92L, 108L, 100L, 107L, 81L, 37L, 47L, 70L, 114L,
125L, 99L, 46L, 108L, 106L, 111L, 75L, 75L, 136L, 36L, 13L, 35L,
71L, 105L, 113L, 116L, 116L, 94L, 130L, 102L, 19L, 1L, 33L, 78L,
89L, 115L, 130L, 117L, 105L, 0L, 36L, 80L, 130L, 124L, 124L,
66L, 75L, 57L, 94L, 124L, 165L, 170L, 158L, 127L, 116L, 107L,
118L, 150L, 96L, 101L, 59L, 38L, 46L, 77L, 83L, 54L, 61L, 60L,
55L, 110L, 127L, 129L, 87L, 61L, 70L, 85L, 110L, 131L, 151L,
123L, 50L, 77L, 100L, 135L, 151L)), .Names = c("Date", "Score"
), row.names = c(NA, 100L), class = "data.frame")
I am trying to write code that will check to see if the score is above a
certain point, and then if it is, calculate how long it stays above that
point.
I'm not incredibly familiar with R, or programming in general. Here is my
attempt:
# TimeDiff is supposed to be a vector containing time objects. The first is
subtracted from the last to give a time difference.
#TimeTable is supposed to be a vector of time differences.
for(i in seq(along=Data$Score)){
if(Data$Score[i]>=140){
(Data$Date[i])->TimeDiff[length(TimeDiff)+1]}
# I put the calculation of the time difference in this loop because when the
score drops below 140, that”run” of high scores is ended.
if(Data$Score[i]<140){
if(TimeDiff != 0){
TimeTable[length(TimeTable)+1]<-TimDiff[length(TimeDiff)]-TimeDiff[2]
TimeDiff<-0
}
}
}
I'm certain there is a more elegant and efficient way to do this, but this
should at least be functional. It isn't, however. If I copy and paste this
into R, I don't get any unexpected symbol errors. However, I do get a
message saying that there were 50 or more warnings. They're all:
“In if (TimeDiff != 0) { ... :
the condition has length > 1 and only the first element will be used”
I don't know what that means, nor how to fix it.
Additionally, TimeDiff ends up being a bunch of numbers of numeric class.
However, when I manually subtract one time from another, I get a time
difference (which is what I'm looking for).
Also, TimeTable ends up being 0 at the end, instead of a vector of time
differences
Any help on getting this working would be greatly appreciated.
--
View this message in context: http://r.789695.n4.nabble.com/Script-help-Determining-Time-Difference-between-two-data-points-tp4636743.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list