【发布时间】:2023-04-02 08:40:01
【问题描述】:
我有一个数字向量,我需要将区间作为向量列表。
我认为这很容易,但我真的很难找到一个好的、简单的方法。
一种糟糕而复杂的方法是粘贴向量及其滞后,然后拆分结果。
这是有效但丑陋的代表:
library(tidyverse)
xx = c(1, 5, 10 ,15 ,20)
paste0(lag(xx), "-", xx-1) %>% str_split("-") #nevermind the first one, it cannot really make sense anyway
#> [[1]]
#> [1] "NA" "0"
#>
#> [[2]]
#> [1] "1" "4"
#>
#> [[3]]
#> [1] "5" "9"
#>
#> [[4]]
#> [1] "10" "14"
#>
#> [[5]]
#> [1] "15" "19"
由reprex package (v0.3.0) 于 2020-09-06 创建
有没有更简洁的方法来做同样的事情?
【问题讨论】:
标签: r