我有一个由两列组成的数据框
positionx <- c(1:10)
pvalue <- c(0.1, 0.04, 0.03, 0.02, 0.001, 0.2, 0.5, 0.6, 0.001, 0.002)
df <- data.frame(cbind(positionx, pvalue))
df
positionx pvalue
1 1 0.100
2 2 0.040
3 3 0.030
4 4 0.020
5 5 0.001
6 6 0.200
7 7 0.500
8 8 0.600
9 9 0.001
10 10 0.002
我想找出我的p值在positionx值的哪个区间内低于某个阈值,例如0.05。使用“哪个”,我可以找到行的索引,并且可以返回到positionx的视图。
which(df[,2]<0.05)
[1] 2 3 4 5 9 10
但是我想要的是间隔的边缘,我的意思是这样的结果:2-5,9-10
我也尝试如下使用findInterval函数
int <- c(-10, 0.05, 10)
separation <- findInterval(pvalue,int)
separation
[1] 2 1 1 1 1 2 2 2 1 1
df_sep <- data.frame(cbind(df, separation))
df_sep
positionx pvalue separation
1 1 0.100 2
2 2 0.040 1
3 3 0.030 1
4 4 0.020 1
5 5 0.001 1
6 6 0.200 2
7 7 0.500 2
8 8 0.600 2
9 9 0.001 1
10 10 0.002 1
然而,我再次陷入一列数字,但我希望分隔列中包含1的区间的边缘。有办法吗?
这是一个简化的示例,实际上我有很多图,并且对于每个图都有一个这种类型的数据帧(只是更长的时间,而p值一眼就不容易判断)。我认为我需要时间间隔边缘的信息的原因是,我想根据pvalue为ggplot的背景着色。我知道我可以使用geom_rect,但是我想我需要间隔的边缘才能构建彩色矩形。
是否有一种方法可以自动执行而不是手动执行?
这似乎是run length encoding的一个很好的用例。
示例如下: