用复合键指示每行多个指标变量的有效方式? [英] An efficient way to indicate multiple indicator variables per row with composite key?
本文介绍了用复合键指示每行多个指标变量的有效方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的指标和值对象具有映射到彼此的复合键,是否有有效的方式将值聚合到指标对象中?
My indicator and value objects have composite keys that map to each other is there an efficient way to aggregate the values into the indicator object?
给定一个空指标数据框:
Given an "empty" indicator dataframe:
indicator <- data.frame(Id1=c(1,1,2,2,3,3,4,4), Id2=c(10,11,10,12,10,12,10,12),Ind_A=rep(0,8),Ind_B=rep(0,8))
Id1 Id2 Ind_A Ind_B
1 1 10 0 0
2 1 11 0 0
3 2 10 0 0
4 2 12 0 0
5 3 10 0 0
6 3 12 0 0
7 4 10 0 0
8 4 12 0 0
和值的数据框:
values <- data.frame(Id1=c(1,1,1,2,2,3,3,4,4,4),Id2=c(10,10,11,10,12,10,12,10,10,12),Indicators=c('Ind_A','Ind_B','Ind_A','Ind_B','Ind_A','Ind_A','Ind_A','Ind_A','Ind_B','Ind_A'));
Id1 Id2 Indicators
1 1 10 Ind_A
2 1 10 Ind_B
3 1 11 Ind_A
4 2 10 Ind_B
5 2 12 Ind_A
6 3 10 Ind_A
7 3 12 Ind_A
8 4 10 Ind_A
9 4 10 Ind_B
10 4 12 Ind_A
我想要结束:
Id1 Id2 Ind_A Ind_B
1 10 1 1
1 11 1 0
2 10 0 1
2 12 1 0
3 10 1 0
3 12 1 0
4 10 1 1
4 12 1 0
推荐答案
您可以使用 dcast
将值数据集从长格式转换为宽格式。
You could use dcast
to convert the "values" dataset from 'long' to 'wide' format.
library(reshape2)
dcast(values, Id1+Id2~Indicators, value.var='Indicators', length)
# Id1 Id2 Ind_A Ind_B
#1 1 10 1 1
#2 1 11 1 0
#3 2 10 0 1
#4 2 12 1 0
#5 3 10 1 0
#6 3 12 1 0
#7 4 10 1 1
#8 4 12 1 0
如上所示,您可能不需要创建第二个数据集,但如果需要更改值在一个基于其他数据集的数据集中,
As showed above, you may not need to create a second dataset, but if you need to change the values in one dataset based on the value in other,
indicator$Ind_A <- (do.call(paste, c(indicator[1:2], 'Ind_A')) %in%
do.call(paste, values))+0L
indicator$Ind_B <- (do.call(paste, c(indicator[1:2], 'Ind_B')) %in%
do.call(paste, values))+0L
这篇关于用复合键指示每行多个指标变量的有效方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文