簡體   English   中英

根據長度分割字符向量

[英]Split character vector based on length

我有一個像下面這樣的字符向量。

text <- c(
  "My test",
  "Test2",
  "Tests",
  "Dolphin Sentimental S.r.l.", "Tiger Sentiyapa S.r.l.", 
  "Effort rate calculates to grant (Debt to Income Rate)", 
  "Amount of pensions received mens.", 
  "(Grant data) (Pension Received (Monthly Basis))", 
  "Effort rate calculates to grant (Debt to Income Rate)", 
  "Amount of pensions received mens.", 
  "(Grant data) (Pension Received (Monthly Basis))"
)

如果不。 整個向量中的字符數(如上所示)大於 100,將其拆分為多個字符向量,其中沒有。 字符數 < 100。我嘗試使用分位數方法,但它不起作用,因為如果您觀察到向量的前 3 個元素與 5 到 11 之間的元素相比包含更少的文本,因此這種方法不可靠且容易出錯。

nRun <- ceiling(sum(nchar(text),na.rm = T)/100)
cutsIter <- ceiling(quantile(1:length(text),probs = seq.int(0,1,(1/nRun))))

新字符向量

text[cutsIter[1]:cutsIter[2]]

所需結果前 5 個元素應位於一個向量中。 6th 和 7th 應該在同一個向量中,然后繼續。

這是您可以做到的一種方法。 我相信有更好的方法,但這個解決方案也可以改進。 為此,我選擇編寫自定義函數。 當只剩下 1 個nchar等於100向量時,仍然存在一個問題。 這應該根據您的喜好進行修復。

out <- c()
x <- nchar(text)

fn <- function(x) {
  
  if(max(cumsum(x)) < 100) {
    ind <- max(which(cumsum(x) < 100))
    return(c(out, length(x)))
  } else {
    ind <- max(which(cumsum(x) < 100))
    out <<- c(out, ind)
  }
  
  x <- x[-c(1:ind)]
  fn(x)
}

# The result of the function is the indices for us to split the vector
tmp <- fn(nchar(text))
tmp
[1] 5 2 1 2 1

如果我們將它應用於我們的矢量text

split(text, rep(seq_len(length(tmp)), tmp))

$`1`
[1] "My test"                    "Test2"                      "Tests"                     
[4] "Dolphin Sentimental S.r.l." "Tiger Sentiyapa S.r.l."    

$`2`
[1] "Effort rate calculates to grant (Debt to Income Rate)"
[2] "Amount of pensions received mens."                    

$`3`
[1] "(Grant data) (Pension Received (Monthly Basis))"

$`4`
[1] "Effort rate calculates to grant (Debt to Income Rate)"
[2] "Amount of pensions received mens."                    

$`5`
[1] "(Grant data) (Pension Received (Monthly Basis))"

最后,如果您想創建盡可能多的向量:

split(text, rep(seq_len(length(tmp)), tmp)) |>
  setNames(paste0("vec", seq_along(tmp))) |>
  list2env(envir = globalenv())

有一個很棒的預定義函數MESS::cumsumbinning()您可以在這些場景中輕松使用

text <- c(
  "My test",
  "Test2",
  "Tests",
  "Dolphin Sentimental S.r.l.", "Tiger Sentiyapa S.r.l.", 
  "Effort rate calculates to grant (Debt to Income Rate)", 
  "Amount of pensions received mens.", 
  "(Grant data) (Pension Received (Monthly Basis))", 
  "Effort rate calculates to grant (Debt to Income Rate)", 
  "Amount of pensions received mens.", 
  "(Grant data) (Pension Received (Monthly Basis))"
)

library(MESS)

split(text, cumsumbinning(nchar(text), 100))
#> $`1`
#> [1] "My test"                    "Test2"                     
#> [3] "Tests"                      "Dolphin Sentimental S.r.l."
#> [5] "Tiger Sentiyapa S.r.l."    
#> 
#> $`2`
#> [1] "Effort rate calculates to grant (Debt to Income Rate)"
#> [2] "Amount of pensions received mens."                    
#> 
#> $`3`
#> [1] "(Grant data) (Pension Received (Monthly Basis))"      
#> [2] "Effort rate calculates to grant (Debt to Income Rate)"
#> 
#> $`4`
#> [1] "Amount of pensions received mens."              
#> [2] "(Grant data) (Pension Received (Monthly Basis))"

不用說,如果您想將上面列表的每個項目保存為單獨的項目,請使用list3env as

split(text, cumsumbinning(nchar(text), 100)) |>
  list2env(envir = .GlobalEnv)


如果您希望您的閾值限制不超過,請在上面使用閾值 99

split(text, cumsumbinning(nchar(text), 99))

$`1`
[1] "My test"                   
[2] "Test2"                     
[3] "Tests"                     
[4] "Dolphin Sentimental S.r.l."
[5] "Tiger Sentiyapa S.r.l."    

$`2`
[1] "Effort rate calculates to grant (Debt to Income Rate)"
[2] "Amount of pensions received mens."                    

$`3`
[1] "(Grant data) (Pension Received (Monthly Basis))"

$`4`
[1] "Effort rate calculates to grant (Debt to Income Rate)"
[2] "Amount of pensions received mens."                    

$`5`
[1] "(Grant data) (Pension Received (Monthly Basis))"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM