[R] find longest consecutive streak in double
Nick Tulli
nick.tulli.95 at gmail.com
Sun Jun 5 21:51:35 CEST 2016
Hey guys. Learning R after gaining a background in Python this year,
and I'm translating my Python projects to R now. This is the first
time I'm posting to the mailing list.
Essentially, I have 92 data points in one double that I created from a
netcdf file. Those 92 data points represent a measurement from a
location over the course of three months. From there I've calculated
the median value of the data, which is 8.534281. My goal here is to
test each data point to see if it exceeds the median value, and, next,
calculate the longest streak of days in the 92-day span in which the
data exceeded the median value.
To achieve this in Python, I've created the following function, where
q is a vector of length 92 and q_JJA_median is, of course, the median
value of the dataset.
######################################
def consecutive(q, q_JJA_median):
is_consecutive = 0
n = 0
array_exceed = []
for i in range(len(q)):
if q[i] > q_JJA_median:
n+=1
is_consecutive = 1
else:
if is_consecutive:
array_exceed.append(n)
n = 0
is_consecutive = 0
if q[i] > q_JJA_median:
array_exceed.append(n)
if len(array_exceed) == 0:
array_exceed = 0
if type(array_exceed) is int:
array_exceed = [0]
return array_exceed
#######################################
Here is my work thus far written for R:
#######################################
is_consecutive = 0
n = 0
array_exceed <- c()
for (i in q) {
if (i > q_JJA_median) {
n = n + 1
is_consecutive = is_consecutive + 1
}
else {
if (is_consecutive) {
append(array_exceed, n)
n <- 0
is_consecutive <- 0
}
}
if (i > q_JJA_median) {
append(array_exceed,n)
}
}
#######################################
My code written for R has been changed and manipulated many times, and
none of my attempts have been successful. I'm still new to the syntax
of the R language, so my problem very well could be a product of my
lack of experience.
Additionally, I read on a forum post that using the append function
can be slower than many other options. Is this true? If so, how can I
circumvent that issue?
Thank you!
More information about the R-help
mailing list