gpt4 book ai didi

r - 数字向量和 data.table 中的索引

转载 作者:行者123 更新时间:2023-12-04 07:19:44 26 4
gpt4 key购买 nike

将以下向量与每个数字的索引一起放入 data.table 的最佳方法是什么?

nVector <- c("20 37", "38 23", "39 48", "45 76", "65 44", "86 95 80")

这是我所拥有的,有没有更好的方法?
vLength <- unname(sapply(nVector, function(x) length(unlist( strsplit(x, " "))) ))
vSeq <- seq(1, length(vLength))
vPosition <- vector("list", length(vLength))

for(i in 1:length(vLength))
{
vPosition[[i]] <- rep(vSeq[i], vLength[i])
}

DT <- data.table(index = unlist(vPosition),
value = unlist(strsplit(nVector, " ")))

注意:向量的长度大约为 125k,大约有 2000 万个值。

最佳答案

为什么不简单:

data.table(v1 = nVector)[, index := .I][, list(unlist(strsplit(v1, " "))), by = index]
## index V1
## 1: 1 20
## 2: 1 37
## 3: 2 38
## 4: 2 23
## 5: 3 39
## 6: 3 48
## 7: 4 45
## 8: 4 76
## 9: 5 65
## 10: 5 44
## 11: 6 86
## 12: 6 95
## 13: 6 80

或者,您可以创建一个如下所示的函数(使用函数更方便重用——如果它只是一次性问题,则不需要):
fun <- function(invec) {
x <- strsplit(invec, " ", TRUE)
data.table(index = rep(seq_along(x), lengths(x)), V1 = unlist(x, use.names = FALSE))
}

fun(nVector)

注意使用 fixed = TRUE会给你一个很好的速度提升——所以即使在“data.table”方法中你也应该考虑这一点。

最后,正如@Jaap 建议的那样,您可以使用 cSplit从我的“splitstackshape”包中,像这样:
library(splitstackshape)
cSplit(data.table(v1 = nVector)[, index := .I], "v1", sep = " ", direction = "long")

更新

由于考虑到数据的大小,性能是一个问题,因此您可能希望使用 fun方法,您手动创建“data.table”。

以下是较大版本向量的一些时间:
NVector <- rep(nVector, 10000)
length(NVector)
# [1] 60000

f1 <- function(invec) {
data.table(v1 = invec)[, index := .I][
, list(unlist(strsplit(v1, " ", TRUE))), by = index]
}

f2 <- function(invec) {
cSplit(data.table(v1 = invec)[, index := .I],
"v1", sep = " ", direction = "long")
}

library(microbenchmark)
microbenchmark(fun(NVector), f1(NVector), f2(NVector), times = 50)
# Unit: milliseconds
# expr min lq mean median uq max neval
# fun(NVector) 13.26559 13.70738 15.89918 14.12573 15.11083 50.84675 50
# f1(NVector) 196.95570 207.60004 223.74729 212.49649 224.78725 378.51007 50
# f2(NVector) 167.38512 176.16370 196.28389 183.96098 202.00187 412.71760 50

更新:2017 年 12 月 28 日

其中任何一个的性能可能至少部分取决于产生的分割件的数量,所以我想我会:
  • 用一些更不规则的数据更新答案
  • 添加更多选项,特别是:
  • 另一种基本 R 方法
  • fun 的替代方案使用 stringi
  • 一种可能的“tidyverse”方法

  • 这是新的示例数据:
    library(stringi)
    set.seed(2)
    NVec2 <- vapply(sample(20, 60000, TRUE),
    function(x) paste(stri_rand_strings(x, 5, "[0-9]"), collapse = " "),
    character(1L))

    length(NVec2)
    # [1] 60000

    以下是新功能:
    ## like `fun`, but using `stri_split_fixed`
    fun_stringi <- function(invec) {
    x <- stri_split_fixed(invec, " ")
    data.table(index = rep(seq_along(x), lengths(x)), V1 = unlist(x, use.names = FALSE))
    }

    ## A base R alternative
    f3 <- function(invec) stack(setNames(strsplit(invec, " ", TRUE), seq_along(invec)))

    ## A tidyverse approach
    f4 <- function(invec) {
    data_frame(ind = seq_along(invec),
    val = stri_split_fixed(invec, " ")) %>%
    unnest()
    }

    以及新的基准测试:
    library(microbenchmark)
    res <- microbenchmark(base = fun(NVec2), stringi = fun_stringi(NVec2),
    data_table = f1(NVec2), splitstackshape = f2(NVec2),
    base_alt = f3(NVec2), tidyverse = f4(NVec2), times = 50)
    res
    # Unit: milliseconds
    # expr min lq mean median uq max neval
    # base 162.6149 174.7311 204.0177 187.3446 213.7267 443.8357 50
    # stringi 146.8655 157.6717 187.1125 168.5383 192.1952 394.1169 50
    # data_table 360.0788 382.9118 427.2276 396.0421 418.1821 598.3754 50
    # splitstackshape 542.8882 578.6317 619.9677 598.5113 626.5734 901.9400 50
    # base_alt 259.2847 293.7944 325.6021 310.7322 339.1613 492.4644 50
    # tidyverse 500.1571 519.4765 545.4757 534.1167 549.4756 713.3711 50

    进一步增加数据以更接近于模拟您的实际数据集,性能确实开始收敛——除了“splitstackshape”,它的速度非常慢:-(

    这是一个示例:
    library(stringi)
    set.seed(2)
    NVec3 <- vapply(sample(100:200, 125000, TRUE),
    function(x) paste(stri_rand_strings(x, 5, "[0-9]"), collapse = " "),
    character(1L))

    system.time({out <- f2(NVec3)})
    # user system elapsed
    # 20.89 0.03 20.94

    ## Similar to your actual data
    length(NVec3)
    # [1] 125000
    nrow(out)
    # [1] 18767938

    res <- microbenchmark(base = fun(NVec3), stringi = fun_stringi(NVec3),
    data_table = f1(NVec3), base_alt = f3(NVec3),
    tidyverse = f4(NVec3), times = 20)
    res
    ## Unit: seconds
    ## expr min lq mean median uq max neval
    ## base 4.967281 5.606208 5.983120 5.978414 6.345823 7.189997 20
    ## stringi 4.888080 5.292926 5.811898 5.728464 6.091029 7.923210 20
    ## data_table 5.625772 5.861431 6.244174 6.092079 6.420082 7.698534 20
    ## base_alt 4.635496 5.015382 5.564661 5.486531 6.090838 7.034357 20
    ## tidyverse 5.634781 6.186927 6.717203 6.613003 7.198013 8.154297 20

    autoplot(res, log = FALSE)

    enter image description here

    此时,假设您不太可能重复执行此操作,那么任何选项都应该非常适合实际使用。我个人很惊讶 stack优于所有其他选项......

    关于r - 数字向量和 data.table 中的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34423117/

    26 4 0