为什么lapply()不保留我的data.table键? [英] Why does lapply() not retain my data.table keys?

查看:91
本文介绍了为什么lapply()不保留我的data.table键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆data.tables在列表中。我想对列表中的每个data.table应用 unique(),但这样做会破坏我所有的data.table键。

I have a bunch of data.tables in a list. I want to apply unique() to each data.table in my list, but doing so destroys all my data.table keys.

这里有一个例子:

A <- data.table(a = rep(c("a","b"), each = 3), b = runif(6), key = "a")
B <- data.table(x = runif(6), b = runif(6), key = "x")

blah <- unique(A)

在这里, blah 仍然有一个键,并且一切都在世界上是正确的:

Here, blah still has a key, and everything is right in the world:

key(blah)

# [1] "a"

但是如果我把data.tables添加到一个列表并使用 lapply(),键会被销毁:

But if I add the data.tables to a list and use lapply(), the keys get destroyed:

dt.list <- list(A, B)

unique.list <- lapply(dt.list, unique) # Keys destroyed here

lapply(unique.list, key) 

# [[1]]
# NULL

# [[2]]
# NULL

这可能与我没有真正理解什么意味着键

This probably has to do with me not really understanding what it means for keys to be assigned "by reference," as I've had other problems with keys disappearing.

因此:


  • 为什么lapply不保留我的密钥?

  • 说明密钥是通过引用分配的? >
  • 我是否应该在列表中存储data.tables?

  • 如何安全地存储/操作data.tables而不用担心丢失我的键? li>
  • Why does lapply not retain my keys?
  • What does it mean to say keys are assigned "by reference"?
  • Should I even be storing data.tables in a list?
  • How can I safely store/manipulate data.tables without fear of losing my keys?

编辑

c> c>的可怕的也很好:

For what it's worth, the dreaded for loop works just fine, too:

unique.list <- list()

for (i in 1:length(dt.list)) {
  unique.list[[i]] <- unique(dt.list[[i]])
}

lapply(unique.list, key)

# [[1]]
# [1] "a"

# [[2]]
# [1] "x"


$ b b

但是这是R,循环是 evil

推荐答案

有趣的是,注意这两个不同结果之间的区别

Interestingly, notice the difference between these two different results

lapply(dt.list, unique) 
lapply(dt.list, function(x) unique(x)) 

如果你使用后者,结果是你所期望的。

If you use the latter, the results are as you would expect.

看似意外的行为是由于第一个 lapply 语句是
调用 unique.data.frame (即从 {base} ),正在调用 unique.data.table

The seemingly unexpected behavior is due to the fact that the first lapply statement is invoking unique.data.frame (ie from {base}) while the second is invoking unique.data.table

这篇关于为什么lapply()不保留我的data.table键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆