我有一个时间序列,我想标记所有低于10的值,该值也至少有17个其他邻居(从左侧或右侧或两侧)都低于10。
我有一个for
循环的解决方案,当数据集很大时,它将永远占用。
我感谢使用dplyr
或apply
系列功能来加快速度的其他选择:
w = c(4.38, 4.03, 4.35, 4.18, 4.44, 4.46, 4.06, 3.99, 4.19, 3.88,
4.19, 3.86, 4.38, 3.89, 3.99, 3.75, 3.66, 3.56, 3.89, 3.39, 2.76,
2.93, 2.81, 2.74, 2.62, 2.85, 3.5, 3.63, 4.28, 4.67, 4.45, 4.87,
4.93, 4.99, 5.19, 5.2, 5.64, 5.38, 6.26, 5.73, 5.76, 5.46, 5.66,
6, 5.89, 5.93, 5.63, 5.63, 5.16, 5.38, 5.26, 4.57, 4.24, 3.77,
4.13, 3.77, 3.52, 3.37, 3.14, 2.71, 3.24, 3.12, 2.98, 3.18, 2.6,
2.88, 3.21, 2.53, 2.9, 2.93, 2.69, 2.37, 1.91, 1.39, 1.61, 0.95,
0.16, 0.39, 1.11, 0.92, 0.32, 0.89, 1.01, 0.83, 1.48, 1.55, 1.05,
1.64, 2.46, 2.37, 2.56, 3.26, 3.2, 3.75, 4.08, 4.13, 4.08, 4.19,
4.22, 4.78, 5.26, 5.56, 5.4, 5.87, 5.58, 6.87, 7.8, 7.64, 8,
7.62, 7.8, 7.74, 7.55, 7.56, 7.78, 7.35, 7.66, 7.61, 7.95, 8.29,
7.99, 8.41, 8.14, 8.53, 8.97, 8.14, 8.53, 9.23, 8.67, 8.41, 8.15,
8.36, 7.54, 7.81, 7.71, 7.61, 7.38, 7.59, 7.39, 7.67, 7.53, 7.85,
8.39, 8.32, 8.22, 7.95, 7.92, 8.52, 9.2, 9.3, 9.4, 8.31, 9.82,
9.39, 9.03, 8.52, 7.78, 7.7, 7.5, 7.72, 7.34, 6.97, 7.28, 7.48,
7.41, 7.14, 7.13, 7.17, 7.72, 7.42, 7.17, 7.11, 7, 6.93, 6.94,
6.66, 6.82, 6.82, 6.76, 6.1, 6.04, 6.74, 7.95, 7.25, 7.2, 7.21,
7.31, 6.63, 7, 6.75, 6.54, 7.67, 7.91, 7.86, 7.12, 7.79, 8.51,
8.41, 9.34, 8.27, 8.58, 9.55, 9.18, 9.36, 9.41, 9.97, 9.73, 9.55,
8.8, 8.55, 8.58, 8.12, 7.15, 7.95, 8.12, 8.14, 7.79, 7.76, 8.21,
7.16, 6.86, 7.54, 7.91, 7.84, 7.67, 7.18, 7.29, 7.69, 7.23, 7.12,
7.36, 7.6, 7.33, 6.54, 6.22, 6.73, 6.49, 5.98, 5.91, 5.79, 6.39,
5.72, 6.41, 6.29, 6.26, 6.75, 7.2, 7.08, 7.06, 7.08, 7.04, 6.69,
7.03, 7.57, 7.49, 7.26, 7.89, 7.9, 7.62, 7.21, 7.33, 7.72, 8.11,
7.71, 8.27, 8.42, 8.76, 8.52, 9.06, 8.39, 8.18, 7.68, 7.89, 7.41,
7.09, 7.53, 7.43, 8.06, 7.52, 8.18, 7.42, 7.36, 7.73, 7.99, 7.66,
6.96, 7.93, 7.13, 7.35, 7.36, 7.18, 7.01, 7.29, 7.28, 7.25, 7.53,
7.91, 7.38, 7.39, 7.27, 7.31, 7.09, 6.82, 6.4, 5.96, 5.59, 5.23,
5.44, 4.66, 4.78, 5.4, 5.26, 5.72, 5.15, 4.64, 4.97, 5.18, 5.14,
4.64, 4.43, 3.83, 3.77, 3.63, 3.1, 2.87, 2.77, 2.65, 2.85, 2.8,
2.36, 2.4, 2.6, 3.02, 3.31, 3.22, 3.19, 2.81, 2.42, 2.36, 1.96,
2.71, 3.47, 3.64, 3.4, 3.49, 3.28, 2.8, 2.75, 2.56, 2.9, 2.76,
2.94, 3.19, 3.59, 3.7, 3.57, 3.41, 3.3, 2.75, 2.49, 2.68, 2.76,
2.63, 2.51, 2.74, 2.86, 2.57, 2.26, 3.18, 3.71, 3.57, 3.22, 3.12,
2.76, 2.37, 1.42, 0.87, 0.87, 1.4, 0.76, 2.72, 3.56, 3.14, 3.09,
3.16, 2.62, 3.21, 2.59, 4.31, 4.47, 4.39, 4.83, 4.86, 4.69, 5.45,
5.74, 5.3, 5.42, 5.34, 4.92, 5.71, 5.05, 5.44, 5.78, 4.79, 4.81,
4.37, 4.26, 3.71, 3.76, 3.89, 3.92, 3.81, 3.36, 3.73, 3.99, 3.57,
3.44, 3.91, 3.94, 4.05, 3.88, 3.78, 4.1, 3.96, 4.13, 3.22, 2.73,
2.06, 2.2, 2.06, 2.6, 2.64, 2.02, 0.99, 0.86, 1.26, 2.07, 2.51,
3.71, 3.74, 1.58, 2.47, 1.08, 1.16, 1.7, 1.47, 1.49, 1.19, 1.87,
2.34, 2.46, 3.25, 3.55, 5.35, 7.55, 7.67, 7.09, 7.64, 7.58, 6.95,
7.66, 8.29, 8.69, 9.95, 10.25, 7.93, 5.93, 4.38, 4.22, 5.91,
4.15, 3.46, 4.52, 4.26, 4.08, 4.17, 3.58, 4.26, 4.37, 4.73, 7.35,
7.3, 8.11, 8.81, 9.21, 8.95, 11.59, 11.76, 10.5, 9.7, 10.56,
9.88, 9.8, 9.78, 8.09, 7.73, 8.07, 7.64, 8.89, 9.11, 8.39, 7.74,
7.87, 7.29, 7.84, 7.58, 6.78, 6.06, 5.26, 4.06, 3.12, 2.38, 2.37,
2.11, 1.45, 3.53, 3.92, 4.38, 4.03, 3.07, 3.57, 3.1, 2.87, 2.39,
2.38, 2.1, 1.75, 1.38, 1.89, 1.24, 1.34, 1.57, 1.61, 1.99, 2.09,
2.81, 2.79, 2.61, 3.65, 2.08, 2.92, 4.8, 5.85, 5.87, 6.46, 6.96,
5.96, 6.54, 7.37, 9.12, 9.25, 9.69, 9.42, 8.65, 7.9, 7.45, 7.63,
8.12, 7.24, 7.12, 6.88, 5.96, 5.01, 3.25, 2.62, 2.2, 3.36, 3.34,
3.67, 3.41, 3.37, 4.09, 3.59, 2.88, 2.53, 2.5, 2.04, 1.16, 1.47,
0.63, 0.18, 0.18, 0.39, 0.89, 1.36, 2.37, 3.05, 3.56, 3.72, 3.31,
2.06, 1.66, 1.75, 1.3, 0.24, 0.18, 0.18, 1.42, 4.1, 4.05, 3.65,
4.43, 4.74, 4.71, 4.35, 4.23, 4.23, 4.25, 4.31, 4.73, 4.29, 2.42,
1.84, 1.9, 2.38, 1.4, 1.15, 1.77, 1.99, 2.04, 2.06, 2.8, 2.71,
3.05, 2.6, 2.73, 2.31, 0.1, 0.29, 1.21, 2.3, 2.75, 4.26, 4.74,
4.37, 3.56, 2.6, 2.39, 3.74, 6.03, 6.09, 5.86, 5.35, 6.63, 6.2,
6.11, 5.84, 5.81, 5.52, 5.15, 4.98, 4.9, 5.04, 4.41, 4.46, 3.89,
3.89, 4.37, 4.7, 6.1, 6.66, 5.13, 5.2, 5.33, 5.14, 5.36, 6.32,
6.12, 6.34, 6.53, 6.39, 6.36, 6.53, 6.26, 5.68, 5.14, 5.51, 6.45,
7.38, 7, 7.14, 6.92, 7.39, 8.32, 8.16, 8.63, 8.51, 7.95, 8.24,
7.6, 7.72, 7.75, 7.73, 8.01, 7.37, 7.74, 8.76, 9.99, 10.35, 10.23,
9.34, 9.23, 9.66, 9.3, 8.26, 9.48, 10.14, 9.9, 9.6, 9.42, 7.86,
7.92, 8, 7.78, 7.49, 7.45, 7.87, 7.49, 6.27, 6.14, 6.56, 6.75,
7.39, 7.04, 6.51, 7.28, 8.27, 8.07, 7.86, 7.52, 7.87, 7.04, 6.97,
7.41, 7.49, 7.42, 7.06, 7.75, 8.57, 8.97, 8.65, 8.82, 8.18, 8.1,
8.15, 8.29, 8.62, 8.53, 9.76, 9.63, 9.66, 9.28, 8.85, 8.94, 9.64,
9.66, 9.32, 9.58, 10.48, 10.75, 10.35, 10.06, 9.63, 9.59, 9.87,
9.9, 9.34, 9.4, 9.29, 9.33, 9.53, 9.87, 9.9, 9.91, 10.32, 9.68,
9.44, 8.88, 8.58, 9.07, 9.15, 10.52, 10.65, 11.42, 10.97, 11.29,
11.38, 11.67, 10.08, 10.58, 10.52, 10.29, 9.24, 9.46, 10.22,
9.77, 10.08, 9.85, 9.23, 9.66, 9.56, 9.12, 8.53, 9.08, 9.36,
10.18, 9.87, 10.12, 9.32, 9.12, 8.47, 8.3, 8.01, 7.79, 7.91,
7.57, 7.9, 7.66, 8.06, 7.81, 8.27, 8.61, 8.7, 9.09, 9.33, 9.06,
9.82, 10.64, 10.84, 10.85, 11.34, 11.39, 11.41, 11.58, 11.66,
10.91, 11.39, 11.65, 11.52, 11.87, 11.85, 12.1, 12.52, 13.38,
13.24, 12.55, 12.31, 12.65, 12.05, 11.75, 12.08, 11.29, 12.53,
12.21, 11.98, 11.08, 10.93, 10.89, 11.25, 10.86, 9.85, 10.55,
11.11, 10.96, 11.51, 12.39, 11.98, 11.24, 10.78, 10.05, 10.21,
9.02, 8.58, 8.55, 8.68, 7.01, 6.01, 5.33, 5.39, 5.32, 6.18, 7.11,
7.24, 6.72, 6.17, 7.28, 7.36, 7.27, 8.08, 8.48, 7.2, 9.06, 8.13,
9.75, 8.74, 9.4, 9.88, 9.57, 9.34, 9.41, 9.32, 9.05, 9.76, 10.13,
10.37, 9.68, 9.95, 9.69, 9.38, 9.15, 10, 10.04, 9.85, 10, 10.21,
10.44, 10.44, 11.23, 10.52, 10.66, 11.07, 10.89, 9.57, 9.62,
9.45, 10.72, 10.37, 11.24, 12.22, 12.04, 11.35, 11.06, 11.11,
10.36, 10.6, 10.91, 10.66, 10.98, 11.23, 11.23, 11.56, 10.93,
10.57, 10.83, 11.02, 11.22, 10.83, 9.67, 9.65, 9.15, 9.18, 8.83,
9.39, 8.76, 9.17, 9.05, 9.49, 9.05, 9.3, 9.32, 8.93, 9.39, 9.7,
9.9, 9.75, 9.68, 9.78, 9.85, 9.74, 9.84, 9.77, 10.07, 10.11,
10.12, 9.96, 10.16, 9.45, 9.53, 9.63, 9.84, 8.98, 8.98, 8.67,
8.67, 8.78, 9.16, 8.93, 8.92, 8.97, 9.73, 9.26, 9.03, 8.6, 8.68,
8.52, 8.78, 9.76, 10.55, 11.61)
这是带有for循环的代码:
ind1=ind2=numeric(length(w))
for (i in 1:length(w)) {
ind1[i] = (which(w[i:length(w)] > 10)[1])
ind2[i] = i-(ifelse(length(tail(which(w[1:i] > 10),n=1))==0,NA,tail(which(w[1:i] > 10),n=1)))
}
s= ind1+ind2
a1 = which(is.na(s) | s > 17)
plot(w,t='l')
points(a1,w[a1],col=2)