r - 如何从 R 中的多个集合中创建一个多重集合?
问题描述
我有多个集合,我想在 R 中构建他们的多个集合。有什么解决方案吗?例如,我有 3 套:
set1 <- c(1,3,6)
set2 <- c(1,2,6,7,9)
set3 <- c(1,3,7)
我想要一个像
multiset = {1:3,2:1,3:2,6:2,7:2,9:1}
where1:3
表示该元素1
重复3
多次。
解决方案
Does the following what you want?
set1 <- c(1,3,6)
set2 <- c(1,2,6,7,9)
set3 <- c(1,3,7)
tb <- table(c(set1, set2, set3))
paste(names(tb), m[,1], collapse=",", sep=":")
Edit
Following the comment, that the output should be manipulatable, one can easily put it in a matrix:
tb <- table(c(set1, set2, set3))
mat <- cbind(no=names(tb), freq=tb)
And if needed, convert the character columns to numeric:
apply(mat, 2, as.numeric)
#> no freq
#> [1,] 1 3
#> [2,] 2 1
#> [3,] 3 2
#> [4,] 6 2
#> [5,] 7 2
#> [6,] 9 1
推荐阅读
- python - Python:在给定值的数组中查找某个元素
- laravel - 如何在刀片中使用存储库模式
- ios - 未定义符号:Swift.String.unicodeScalars.getter:XCode 13 中的 Swift.String.UnicodeScalarView
- python - 如何从一组预定义的键、组合键中模拟随机键输入?tkinter GUI /窗口不工作
- html - CSS如何在响应模式下最小化屏幕时添加空间?
- excel - 自动刷新
- flutter - 为什么每次我退出并重新登录时我的bottomAppBar都会消失
- math - 计算截断对数正态分布的平均值
- asp.net - 我可以使用 sdk 和运行时文件发布 asp.net 核心吗?
- python - 如何处理 API 调用上的 Keyrror