包含R中有序数字的拆分字符串

时间:2016-07-18 18:48:06

标签: r string split

我有这样的字符串:

"12385402763024590"

它包含交替升序和降序的数字。我想根据这些订单拆分它们。输出将是:

"1238"  "540"  "27"  "630"  "2459" "0"

我们如何在R中做到这一点?

3 个答案:

答案 0 :(得分:2)

以下是使用rleid包中的data.table和来自R的split函数的选项:

library(data.table)
strToNum <- as.numeric(strsplit(s, "")[[1]])
# split the string into numeric vectors

sapply(split(strToNum, c(1, rleid(diff(strToNum) > 0))), paste0, collapse = "")
# calculate the rleid for sequence in ascending or descending order and split the vector 
# based on the run length encoding ID. Since the first element will always be classified 
# into the first sequence, we prepend 1 in the resulting rleid.

#      1      2      3      4      5      6 
# "1238"  "540"   "27"  "630" "2459"    "0" 

答案 1 :(得分:2)

您还可以将两个向量传递给substring

x <- "12385402763024590"
substring(x, c(1,5,8,10,13,17), c(4,7,9,12,16,17))
[1] "1238" "540"  "27"   "630"  "2459" "0" 

也许?

sp1 <- function(x){
   y <- as.numeric(strsplit(x, "")[[1]])
   n <- cumsum(rle(diff(y)<0)$lengths) +1
   substring(x, c(1, n[-length(n)]+1),   n )
}

sp1(x)
[1] "1238" "540"  "27"   "630"  "2459" "0" 

答案 2 :(得分:1)

以下是我自己使用基础R的解决方案:

f <- function(r, char_s){
    cuts <- c(0, which(diff(r) != 1), length(r))
    sapply(seq_along(tail(cuts,-1)), function(x) 
                  paste0(char_s[r[(cuts[x]+1):cuts[x+1]]],collapse=""))
}

char_s <- strsplit(s, "")[[1]]
dif <- c(1,diff(as.numeric(char_s)))

# ascending orders
f(which(dif>0), char_s)
# [1] "1238" "27"   "2459"

# descending orders
f(which(dif<0), char_s)
# [1] "540" "630" "0"