如何折叠R中整数序列中的任何间隙



是否有一个方便的基R或包函数用于折叠或插值一系列数字的整数值的任何间隙?我已经搜索过像dplyr::dense_rank这样的函数,但在这种情况下它们不起作用。下面的代码在一列(列=Grp(中生成一个数字起始序列示例:

> myDF <- data.frame(Grp = c(1,2.1,2.2,4.1,4.2,6.1,9))
> myDF   
Grp
1 1.0    
2 2.1    
3 2.2    
4 4.1
5 4.2
6 6.1
7 9.0

以下是我想要更改输出的方式;下面,我手动添加值,在每个Grp行的右侧添加一列("折叠"(,解释我试图导出的内容:

> myDF   
Grp    Collapse 
1 1.0    Every sequence starts with 1 so leave Grp as is
2 2.1    Integer gap between rows 1-2 is <= 1 so leave Grp as is
3 2.2    Integer gap between rows 2-3 is <= 1 so leave Grp as is
4 3.0    Integer gap between original rows 3-4 is not <= 1 so fill in the gap with the seq integer 
5 4.1    Integer gap between rows 4-5 is <= 1 so leave Grp as is
6 4.2    Integer gap between rows 5-6 is <= 1 so leave Grp as is
7 5.0    Integer gap between original rows 5-6 is not <= 1 so fill in the gap with the seq integer 
8 6.1    Integer gap between row 7-8 is <= 1 so leave Grp as is
9 7.0    Integer gap between original rows 6-7 is not <= 1 so fill in the gap with the seq integer
10 8.0    Integer gap between original rows 6-7 is not <= 1 so fill in the gap with the seq integer
11 9.0    Integer gap between row 10-11 is <= 1 so leave Grp as is

你可以做:

f <- floor(myDF$Grp)
s <- seq(min(f), max(f))
sort(c(myDF$Grp, s[!s %in% f]))
#[1] 1.0 2.1 2.2 3.0 4.1 4.2 5.0 6.1 7.0 8.0 9.0

最新更新