将data.table列的每个组中的值与向量中的值进行匹配 [英] Match values in each group of a data.table column to values in a vector

查看:61
本文介绍了将data.table列的每个组中的值与向量中的值进行匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用data.table包来识别表列中符合某些条件的值。虽然和我设法得到大部分的事情,现在我坚持这个问题:

I recently started to use the data.table package to identify values in a table's column that conform to some conditions. Although and I manage to get most of the things done, now I'm stuck with this problem:

我有一个数据表,table1,其中第一列标签)是组ID,第二列o.cell是整数。关键是在标签

I have a data table, table1, in which the first column (labels) is a group ID, and the second column, o.cell, is an integer. The key is on "labels"

我有另一个数据表,table2,包含一列:cell。

I have another data table, table2, containing a single column: "cell".

现在,我试图为table1中的每个组查找表2中单元格列中o.cell列中的值。 table1有大约400K行分为800+组不等大小。 table2有大约130万行的唯一单元格编号。

Now, I'm trying to find, for each group in table1, the values from the column "o.cell" that are in the "cell" column of table2. table1 has some 400K rows divided into 800+ groups of unequal sizes. table2 has about 1.3M rows of unique cell numbers. Cell numbers in column "o.cell" table1 can be found in more than one group.

这看起来像一个简单的任务,但我找不到正确的方法做它。根据我结构我的调用的方式,它或者给我一个不同的结果,我期望或它从来没有完成,我必须结束R任务,因为它冻结(我的机器有24 GB RAM)。

This seems like a simple task but I can't find the right way to do it. Depending on the way I structure my call, it either gives me a different result than what I expect or it never completes and I have to end R task because it's frozen (my machine has 24 GB RAM).

这里是我尝试过的调用的一个变体的例子:

Here's an example of one of the "variant" of the calls I have tried:

overlap <- table1[, list(over.cell =
              o.cell[!is.na(o.cell) & o.cell %in% table2$cell]),
              by = labels]

我确定这是使用数据表执行此任务的错误方法,我不能得到我想要的结果。

I pretty sure this is the wrong way to use data tables for this task and on top of that I can't get the result I want.

我将非常感谢任何帮助。感谢。

I will greatly appreciate any help. Thanks.

推荐答案

听起来就像这样设置:

dt1 = data.table(labels = c('a','b'), o.cell = 1:10)
dt2 = data.table(cell = 4:7)

你只是想做一个简单的合并:

And you simply want to do a simple merge:

setkey(dt1, o.cell)
dt1[dt2]
#   o.cell labels
#1:      4      b
#2:      5      a
#3:      6      b
#4:      7      a

这篇关于将data.table列的每个组中的值与向量中的值进行匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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