gpt4 book ai didi

r - 两个 data.table 的连接失败

转载 作者:行者123 更新时间:2023-12-02 04:32:39 24 4
gpt4 key购买 nike

我正在尝试使用数据表作为查找表:

> (dt <- data.table(myid=rep(11:12,3),zz=1:6,key=c("myid","zz")))
myid zz
1: 11 1
2: 11 3
3: 11 5
4: 12 2
5: 12 4
6: 12 6
> (id2name <- data.table(id=11:14,name=letters[1:4],key="id"))
id name
1: 11 a
2: 12 b
3: 13 c
4: 14 d

我想要的是

> (res <- data.table(myid=rep(11:12,3),zz=1:6,name=rep(letters[1:2],3),key=c("myid","zz")))
myid zz name
1: 11 1 a
2: 11 3 a
3: 11 5 a
4: 12 2 b
5: 12 4 b
6: 12 6 b

但是,我尝试的加入失败了:

> dt[id2name]
Starting binary search ...done in 0 secs
Error in vecseq(f__, len__, if (allow.cartesian) NULL else as.integer(max(nrow(x), :
Join results in 8 rows; more than 6 = max(nrow(x),nrow(i)). Check for duplicate key values in i, each of which join to the same group in x over and over again. If that's ok, try including `j` and dropping `by` (by-without-by) so that j runs for each group to avoid the large allocation. If you are sure you wish to proceed, rerun with allow.cartesian=TRUE. Otherwise, please search for this error message in the FAQ, Wiki, Stack Overflow and datatable-help for advice.
Calls: [ -> [.data.table -> vecseq

我做错了什么?

PS。我愿意采用任何替代方式来获得结果;执行我想要的操作的最惯用方法是什么( dt 必须仍然是 data.table,但 id2name 可以是映射 int 到的任何内容其他东西 - 只要 int 不被假定为向量索引)。

最佳答案

> dt[id2name, allow.cartesian=T, nomatch=0]
myid zz name
1: 11 1 a
2: 11 3 a
3: 11 5 a
4: 12 2 b
5: 12 4 b
6: 12 6 b

data.table 试图在您无意中连接具有重复值的键时保护您免受伤害。请注意,如果您确定自己知道自己在做什么,错误消息(最终)会告诉您该怎么做。

或者:

> id2name[dt]
id name zz
1: 11 a 1
2: 11 a 3
3: 11 a 5
4: 12 b 2
5: 12 b 4
6: 12 b 6

关于r - 两个 data.table 的连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22699596/

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