【发布时间】:2016-04-13 12:48:23
【问题描述】:
我有两个数据表如下图:
bigrams
w1w2 freq w1 w2
common names 1 common names
department of 4 department of
family name 6 family name
bigrams = setDT(structure(list(w1w2 = c("common names", "department of", "family name"
), freq = c(1L, 4L, 6L), w1 = c("common", "department", "family"
), w2 = c("names", "of", "name")), .Names = c("w1w2", "freq",
"w1", "w2"), row.names = c(NA, -3L), class = "data.frame"))
一元组
w1 freq
common 2
department 3
family 4
name 5
names 1
of 9
unigrams = setDT(structure(list(w1 = c("common", "department", "family", "name",
"names", "of"), freq = c(2L, 3L, 4L, 5L, 1L, 9L)), .Names = c("w1",
"freq"), row.names = c(NA, -6L), class = "data.frame"))
想要的输出
w1w2 freq w1 w2 w1freq w2freq
common names 1 common names 2 1
department of 4 department of 3 9
family name 6 family name 4 5
到目前为止我做了什么
setkey(bigrams, w1)
setkey(unigrams, w1)
result <- bigrams[unigrams]
这给了我w1 的i.freq 列,但是当我尝试对w2 执行相同操作时,i.freq 列会更新以反映w2 的频率。
如何在不同的列中同时获取 w1 和 w2 的频率?
注意:我已经看到data.table Lookup value and translate 和Modify column of a data.table based on another column and add the new column 的解决方案
【问题讨论】:
-
您在寻找 data.table 解决方案吗?否则这应该工作: bigrams$w1freq
-
@chinsoon12 是的,我更愿意使用 data.table 来解决它,因为我计划将解决方案用于更大的数据集。
-
您想要的输出中的
freq列是否正确? -
@Symbolix 不是,但我已经更正了,谢谢
-
这么想 - 现在我的解决方案很有意义:)
标签: r data.table