簡體   English   中英

"如何在 R 中對 bigz 整數向量進行排序?"

[英]How can I sort a vector of bigz integers in R?

我正在使用gmp<\/code>包<\/a>中的任意精度算術在R<\/code>中工作。 這個包以bigz<\/code>形式創建和存儲大整數。 例如,您可以創建一個任意大整數的向量,如下所示:

X <- as.bigz(c("734876349856913169345", "610034193791098", "82348779011105371828395319",
               "810367198176345917234", "92573840155289", "729811850143511981", "51385",
               "358934723", "751938475", "72265018270590", "12838756105612376401932875"));

它涉及強制轉換為字符串並返回,但您可以使用str_sort() 參數numeric = TRUE給出了自然而不是字母數字的排序順序。

library(stringr)
library(gmp)

as.bigz(str_sort(BIGINTEGERS, numeric = TRUE))
Big Integer ('bigz') object of length 11:
 [1] 51385                      358934723                  751938475                  72265018270590             92573840155289             610034193791098           
 [7] 729811850143511981         734876349856913169345      810367198176345917234      12838756105612376401932875 82348779011105371828395319

另一種選擇是mixedsortgtools轉換后character

as.bigz(gtools::mixedsort(as.character(BIGINTEGERS)))
#Big Integer ('bigz') object of length 11:
# [1] 51385                      358934723                  751938475                 
# [4] 72265018270590             92573840155289             610034193791098           
# [7] 729811850143511981         734876349856913169345      810367198176345917234     
#[10] 12838756105612376401932875 82348779011105371828395319

作為“bigz”類的方法包括as.character

grep('as.character', methods(class = 'bigz'), fixed = TRUE, value = TRUE)
#[1] "as.character.bigz"

我編寫了一個函數來做到這一點,首先按位數對大整數進行分組,然后將每個組排序為字符向量。 它並不完全優雅,但它有效:

library(gmp)
X <- as.bigz(c("734876349856913169345", "610034193791098", "82348779011105371828395319",
               "810367198176345917234", "92573840155289", "729811850143511981", "51385",
               "358934723", "751938475", "72265018270590", "12838756105612376401932875"))

sortbigz <- function(N, decreasing = FALSE) { 
  stopifnot(is.bigz(N))
  # returns a list with the following:
  #  [[1]] a bigz vector, sorted as if NA represented infinity
  #  [[2]] the original argument, converted to a character vector, unsorted
  #  [[3]] integer vector showing the rank of each element of the original vector, in the sorted vector

  z <- is.na(N)
  Ch <- as.character(N)
  is.na(Ch) <- z
  negnumbers <- N < 0
  negnumbers[z] <- FALSE
  str.length <- nchar(Ch)
  n.digits <- ifelse(negnumbers, -(str.length - 1L), str.length)  # number of digits in each element, where for example -582 is deemed to have -3 digits
  r <- rank(n.digits, ties.method = "min")
  upr <- unique(r[!negnumbers]) # unique ranks of positive numbers in N
  unr <- unique(r[negnumbers])  # unique ranks of negative numbers in N
  for(s in upr) r[r == s] <- (s - 1L) + rank(Ch[r == s], ties.method = "min")
  for(s in unr) r[r == s] <- (s + sum(r == s)) - rank(Ch[r == s], ties.method = "random") 
  if(decreasing) r <- (1L + length(N)) - r
  list(sorted.bigz   = N[order(r)], 
       unsorted.char = Ch,
       ranking       = r)
}

sortbigz(X)

暫無
暫無

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

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