2012-01-27 82 views
6

我有两个表,我试图从一个查找值来添加到另一个值。目前我使用两个for循环,但它们运行缓慢。我是R新手,知道我应该避免循环来加快速度,但我无法弄清楚如何实现。查找没有循环

表1(几千行,37列):

type cat1 cat2 cat3 ... cat36 
    1 2 3 2   7 
    3 6 2 1   9 
    2 4 6 7   4 
    3 5 7 8   2 
    5 2 2 9   1 
    4 3 1 2   3 
    1 8 1 4   4 
...

表2(36行,5列):

  type1 type2 type3 type4 type5 
cat1  2  3  4  3  8 
cat2  8  5  5  2  6 
cat3  7  5  1  3  5 
... 
cat36  4  7  2  8  9

我想要通过添加适当的值来修改表1的每个值(在5种类型和36种类别中匹配)来自表2。下面是所期望的结果:

type cat1 cat2 cat3 ... cat36 
    1 4 11 9  11 
    3 10 7 2  11 
    2 7 11 12  11 
    3 9 12 9   4 
    5 10 8 14  10 
    4 6 3 5  11 
    1 10 9 11   8 
...

这是我的当前(慢)代码:

for (i in 1:36) { 
    for (j in 1:nrow(Table1)) { 
     Table1[j,i+1] = Table1[j,i+1] + Table2[i,Table1[j,1]] 
    } 
} 
+1

虽然我用了两个环为清楚起见,我会注意到,我可能只是有一个循环通过表1的行,添加从表2将适当列各表1的行。 – 2012-01-27 18:04:41

回答

4

在表1的类型栏表示其在表2添加到在表1的 。因此,使用“类型”一栏为指标到表2行,然后转所产生的基质,所以你可以添加行的行:

Table3 <- cbind(Table1[ , "type"], 
       t(Table2[ , Table1[ , "type"] ]) + Table1[ , -1]) 

(我假设Table 1和表2是矩阵如果他们是您可以使用的数据帧Table1$type而不是Table1[,"type"])。

+0

谢谢。纠正一个错字后,我得到了这个工作。而不是'Table2 [Table1 [,“type”],]'它应该是'Table2 [,Table1 [,“type”]]' – 2012-01-27 19:08:06

+0

糟糕,你是对的。我混合了行和列之间的转置! – Tyler 2012-01-27 19:17:08

1

我会将这两个表转换为“长”而不是两个不同的“宽”格式。在建立两个表之后,您可以在两个表上执行合并(R data.frame模拟到SQL连接),然后对这些值进行简单求和。

这里有一个类似的例子:

## creating some synthetic data 
df1 <- data.frame(type=sample(1:4, 100, replace=TRUE), cat1=sample(1:4, 100, replace=TRUE), cat2=sample(1:4, 100, replace=TRUE),cat3=sample(1:4, 100, replace=TRUE),cat4=sample(1:4, 100, replace=TRUE)) 
df2 <- data.frame(cat=1:4, type1=sample(1:4,4), type2=sample(1:4,4), type3=sample(1:4,4), type4=sample(1:4,4)) 

require(reshape) 

## rearrange df1 
m1 <- melt(df1, id.vars="type") 
m1$cat <- substr(m1$variable, 4,4) 
m1$variable <- NULL 

## rearrange df2 
m2 <- melt(df2, id.vars="cat") 
m2$type <- substr(m2$variable, 5, 5) 
m2$value2 <- m2$value 
m2$variable <- NULL 
m2$value <- NULL 

## now that they are laid out the same, they can be merged 
df3 <- merge(m1, m2) 
df3$newSum <- df3$value + df3$value2