统计r列中多个字符串中的多个字符
我正在尝试计算一列的多行中出现的多个字符 ID 的出现次数。
我有一个独立 ID 的向量
x2<-sample(101:255,200,replace=TRUE)
,在一个单独的数据框中,我有由多个字符串和一些相应 ID 组成的列,请原谅无法创建完整的示例数据集,但数据框中的相应列如下
sample <- c("105 234 213", "147 211 174", "223 250 243", "189 190 153", "137 200 199")
as.data.frame(sample)
所示结果将是一个新的数据框,其输出如下
ID Occurrences
101 3
102 15
103 2
104 26
105 11
... etc
我希望这是有意义的,任何帮助都会很棒
I'm trying to count the number of occurrences of multiple character IDs that occur in multiple rows of one column.
I have a vector of independent IDs
x2<-sample(101:255,200,replace=TRUE)
And in a separate data frame I have column which consists of multiple character strings with some corresponding IDs, please forgive inability to create a full sample data set but the corresponding column in a data frame looks like this
sample <- c("105 234 213", "147 211 174", "223 250 243", "189 190 153", "137 200 199")
as.data.frame(sample)
The finished result would be a new data frame with an output like this
ID Occurrences
101 3
102 15
103 2
104 26
105 11
... etc
I hope this makes sense and any help would be great
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在基本 R 中,您可以执行以下操作:
或者,使用管道表示法以使逻辑更易于遵循:
由 reprex 包 (v2.0.1)
In base R you can do:
Or, in pipe notation to make the logic easier to follow:
Created on 2022-03-21 by the reprex package (v2.0.1)
像这样的东西吗?
Something like this?