简体   繁体   English

如何将没有重复的整数向量转换为多个连续和离散的部分,并使用 R 以特定格式导出?

[英]how to covert a vector of integers with no repeats to multiple consecutive and discrete parts, and export in a specific format using R?

Below is a sample data:下面是一个示例数据:

dat<-c(1:10,14,17:20,30)

My desired output format is:我想要的输出格式是:

1-10,14,17-10,30

I have a large similar vector, that's why I try to use R to make it.我有一个很大的相似向量,这就是为什么我尝试使用 R 来制作它。 Can anyone give some hints\\suggestions?任何人都可以给出一些提示\\建议吗? Thank you very much!非常感谢!

You can use diff to create groups of consecutive values and use it in tapply where you can paste first and last value of each group if the length of the values is greater than 1.您可以使用diff创建连续值的组,并在tapply中使用它,如果值的长度大于 1,您可以粘贴每个组的第一个和最后一个值。

dat <- c(1:10,14,17:20,30)

result <- as.character(tapply(dat, cumsum(c(TRUE, diff(dat) > 1)), function(x) {
  if(length(x) == 1) x else paste(x[1], x[length(x)], sep = '-')
}))

result
#[1] "1-10"  "14"    "17-20" "30"   

If you want output as one string.如果你想输出为一个字符串。

toString(result)
#[1] "1-10, 14, 17-20, 30"

Base R solution (very similar in principle to Ronak's: Base R 解决方案(原则上与 Ronak 的非常相似:

sapply(split(dat, cumsum(c(FALSE, diff(dat) > 1))), function(x){
    paste(unique(range(x)), collapse = "-")
  }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM