gpt4 book ai didi

r - 将环境中的所有data.frames转换为data.tables

转载 作者:行者123 更新时间:2023-12-04 10:58:58 25 4
gpt4 key购买 nike

将所有data.frames转换为data.tables之后立即使用:=时,我会收到警告:

library(data.table) #Win R-3.5.1 x64 data.table_1.12.2
df1 <- data.frame(A=1, B=2)
df2 <- data.frame(D=3)
lapply(mget(ls()), function(x) {
if (is.data.frame(x)) {
setDT(x)
}
})
df1[, rn:=.I]

Warning message: In [.data.table(df1, , :=(rn, .I)) : Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or was created manually using structure() or similar). Avoid names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. If this message doesn't help, please report your use case to the data.table issue tracker so the root cause can be fixed or this message improved.



下面还生成了相同的警告:
df3 <- data.frame(E=3)
df4 <- data.frame(FF=4)
for (l in list(df3, df4)) setDT(l)
df3[, rn:=.I]

一一打字很乏味
df5 <- data.frame(G=5)
setDT(df5)
df[, rn := .I] #no warning

将所有data.frames转换为data.tables的惯用方式是什么?

有关的:
  • Using setDT inside a function
  • Invalid .internal.selfref in data.table
  • 最佳答案

    有点晚了,但是使用eapply()(以及list2env())似乎是一种很棒的方法,并且很少见。当然,这是另一种选择,当然不能断言这是惯用的方式。

    library(data.table)
    df1 <- data.frame(A=1, B=2)
    df2 <- data.frame(D=3)

    list2env(eapply(.GlobalEnv, function(x) {if(is.data.frame(x)) {setDT(x)} else {x}}), .GlobalEnv)

    df1[, rn:=.I]
    df1
    A B rn
    1: 1 2 1

    一些时间安排和内存使用情况:
    set.seed(0L)
    sz <- 1e7
    df1 <- data.frame(A=rnorm(sz))
    df2 <- data.frame(B=rnorm(sz))
    df3 <- copy(df1)
    df4 <- copy(df2)

    microbenchmark::microbenchmark(unit="ms", times=1L,
    assign_mtd = {
    for (x in ls()) {
    if (is.data.frame(get(x))) {
    assign(x, as.data.table(get(x)))
    }
    }
    },
    eval_sub_mtd = {
    for(x in ls()){
    if (is.data.frame(get(x))) {
    eval(substitute(setDT(x), list(x=as.name(x))))
    }
    }
    },
    eapply_mtd = {
    list2env(eapply(.GlobalEnv, function(x) {
    if (is.data.frame(x)) setDT(x) else x
    }), .GlobalEnv)
    }
    )

    时间:
    Unit: milliseconds
    expr min lq mean median uq max neval
    assign_mtd 115.922802 115.922802 115.922802 115.922802 115.922802 115.922802 1
    eval_sub_mtd 3.293358 3.293358 3.293358 3.293358 3.293358 3.293358 1
    eapply_mtd 1.913802 1.913802 1.913802 1.913802 1.913802 1.913802 1

    关于r - 将环境中的所有data.frames转换为data.tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57267980/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com