r的汇总字符串到r中的向量
我有一个数据表test
:
ID | 键 |
---|---|
1 | 2365 |
1 | 2365 |
1 | 3709 |
2 | 6734 |
2 | 1908 |
2 | 4523 |
我想汇总unique key
by id
使用data.table
软件包进入向量。
预期输出:
ID | KEY_ARRAY |
---|---|
1 | “ 2365”,“ 3709” |
2 | “ 6734”,“ 1908”,“ 4523” |
因此,这应该像array_agg
sql函数一样工作。
我尝试了:res< - test [,list(key_array = paste(unique(key),collapse =“,”)),by =“ id”]
,但我只得到一个字符串。但是我需要有机会找到每个向量的长度并使用其一定元素运行(例如,找到两个向量的相交)。
I have a data table test
:
id | key |
---|---|
1 | 2365 |
1 | 2365 |
1 | 3709 |
2 | 6734 |
2 | 1908 |
2 | 4523 |
I want to aggregate unique key
values by id
into vector using data.table
package.
Expected output:
id | key_array |
---|---|
1 | "2365", "3709" |
2 | "6734", "1908", "4523" |
So, this should work like array_agg
sql function.
I tried:res <- test[, list(key_array = paste(unique(key), collapse = ", ")), by = "id"]
, but I get just a string. But I need to have opportunity to find the length of each vector and operate with its certain elements (find the intersection of two vectors for example).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1。基础r
<代码>汇总单线。
由
创建如果用户 @chris的
请注意,两个
c(unique(x))
andas.character(c(unique(x)))
都将输出列表列,因此后者解决方案无论如何都是正确的。2。软件包
data.table
再次单线。
输出是列表列,每个列表成员都是整数向量。保持整数使用
。
由
创建,为了访问向量,使用两个提取器,一个用于提取列,另一个提取向量。
由
创建。
由
1. Base R
This an
aggregate
one-liner.Created on 2022-06-14 by the reprex package (v2.0.1)
But if user @Chris's comment is right then the right solution as follows.
Note that both
c(unique(x))
andas.character(c(unique(x)))
will output a list column, so the latter solution is right anyway.2. Package
data.table
Once again a one-liner.
The output is a list column, with each list member an integer vector. To keep as integers use
instead.
Created on 2022-06-14 by the reprex package (v2.0.1)
Then, in order to access the vectors use two extractors, one to extract the column and the other one to extract the vectors.
Created on 2022-06-14 by the reprex package (v2.0.1)
3.
dplyr
solutionGroup by
id
and collapse the unique strings into one only.Created on 2022-06-14 by the reprex package (v2.0.1)