简体   繁体   English

从旧数据表构建新数据表

[英]building new data.table from the old one

I have a data table DT with columns k,v: (k1, v1), (k2, v2), (k3, v3)...我有一个包含k,v: (k1, v1), (k2, v2), (k3, v3)...列的数据表 DT k,v: (k1, v1), (k2, v2), (k3, v3)...

I would like to group values by key and then do something with them and produce a new data table (or frame) (s1, t1), (s2, t2)... .我想按键对值进行分组,然后对它们进行处理并生成一个新的数据表(或框架) (s1, t1), (s2, t2)...

Each grouped (k, v1, ..vi) set can be mapped to 0 or more rows in the destination data.table dest , which have totally different range of keys, not related to k.每个分组的(k, v1, ..vi)集合可以映射到目标 data.table dest中的 0 或更多行,它们具有完全不同的键范围,与 k 无关。

I understand that I can write: DT[, myfun(k,v), by=k] but I am not sure how that helps me.我知道我可以写: DT[, myfun(k,v), by=k]但我不确定这对我有什么帮助。

For example, assume that my function myfun needs to compute s = prod(v1, ...vi) and then to increase row s by k : dest[s] += k .例如,假设我的函数myfun需要计算s = prod(v1, ...vi)然后将行s增加kdest[s] += k

I am not sure how can I code this in R!我不知道如何在 R 中编码!

You create an summarised version of DT:您创建 DT 的汇总版本:

DT1 <- DT[, prod(v), by=k]

and then use the values of this to increase dest , the $v now equals the s in your post and so identifies the rows to be incremented:然后使用 this 的值增加dest , $v 现在等于您帖子中的s ,因此标识要增加的行:

dest[DT1$v, "columnToBeIncreased"] = dest[DT1$v, "columnToBeIncreased"] +DT1$k

You say dest is a data.table, and you want to increase a row by a given amount.你说 dest 是一个 data.table,你想按给定的数量增加一行。 If you want the whole row (ie all the columns in that row) to be increased, omit the "columnToBeIncreased" (remove even the quotes, but leave the preceeding comma) - otherwise set it to the the name of the column you want to be incremented.如果您希望整行(即该行中的所有列)增加,请省略“columnToBeIncreased”(甚至删除引号,但保留前面的逗号) - 否则将其设置为您想要的列的名称被递增。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM