根据列中的条件将值(从配置表中提取)分配给组 [英] Assign value (stemming from configuration table) to group based on condition in column

查看:63
本文介绍了根据列中的条件将值(从配置表中提取)分配给组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.table DT1 ,其中包含每个区域的值。在配置表 DT2 中,我将每个区域的每个值映射到变量 number_carrots

I have a data.table DT1, containing a value per region. In a configuration table DT2, I have a mapping of each value per region to a variable number_carrots.

是否存在一种优雅的data.table方法,可将列( number_carrots )添加到 DT1 来自 DT2 中的映射吗?我只能通过循环来做到这一点...

Is there an elegant data.table way of adding a column (number_carrots) to DT1 stemming from the mapping in DT2 ? I only managed to do this with a loop ...

举个例子。 DT1 是:

library(data.table)
region1 = c('Europe', 'Europe', 'Africa', 'Africa', 'Europe', 'Africa')
value1  = c(1, 2, 1, 2, 3, 1)
DT1     = data.table(region1, value1)

> DT1
    region1 value1
1:  Europe      1
2:  Europe      2
3:  Africa      1
4:  Africa      2
5:  Europe      3
6:  Africa      1

DT2 为:

region2         = rep(c('Europe', 'Africa'), each = 3)
value2          = c(1:3, 1:3)
number_carrots  = c(10, 20, 30, 5, 15, 30)

DT2             = data.table(region2, value2, number_carrots)

> DT2
   region2 value2 number_carrots
1:  Europe      1             10
2:  Europe      2             20
3:  Europe      3             30
4:  Africa      1              5
5:  Africa      2             15
6:  Africa      3             30

使用 DT2 ,我想在 DT1 中添加一列 number_carrots

Using, the mapping from DT2, I would like to add a column number_carrots to DT1:

> DT1
   region1 value1 number_carrots
1:  Europe      1             10
2:  Europe      2             20
3:  Africa      1              5
4:  Africa      2             15
5:  Europe      3             30
6:  Africa      1              5


推荐答案

OP要求 DT1 中添加列 number_carrots

这可以通过 update join 解决,该更新通过引用修改 DT1

This can be solved with an update join which modifies DT1 by reference

DT1[DT2, on = .(region1 = region2, value1 = value2), number_carrots := i.number_carrots]
DT1




   region1 value1 number_carrots
1:  Europe      1             10
2:  Europe      2             20
3:  Africa      1              5
4:  Africa      2             15
5:  Europe      3             30
6:  Africa      1              5


这篇关于根据列中的条件将值(从配置表中提取)分配给组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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