我有这个数据框:
> new
median
1 3.957587
2 3.957587
3 3.957587
4 3.957587
5 3.957587
6 3.957587
7 3.249960
8 3.249960
9 3.249960
10 3.249960
11 3.249960
12 3.249960
13 2.962515
14 2.962515
15 2.962515
16 2.962515
17 2.962515
18 2.962515
现在,我将这些中间值与此命令进行比较:
# if difference of median values is bigger than 50%, get index of where this is happens
# returns numeric(0) when condition not met
results <- which(c(0, abs(diff(new$median))) > 0.5 * new$median) - 1
这很好。
我现在想做的是,当中位数的差异小于50%(不满足条件)时,只需获取中位数变化的指数即可。我可以这样:
> testing <- which(new$median[-1] != new$median[-length(new$median)])
> testing
[1] 6 12
我把这两个想法写成了ifelse声明:
> results <- ifelse(length(results) == 0, testing, results)
> results
[1] 6
但这只是给我第一个数字,而不是第二个。为什么?