r - 在R中使用过滤器后如何在表格中显示具有非零频率的组合
问题描述
在 R 中使用过滤器后,我想在表格中显示具有非零频率的组合。
任何帮助将不胜感激。谢谢。
library(tidyverse)
library(tidytext)
library(gtools)
library(magicfor)
df = as.data.frame(rbind(c(1,0830,1,3,5), c(2,0845,3,4,5), c(3,0900,2,4,6), c(4,0915,1,3,6), c(5,0930,2,4,5)))
colnames(df) = c("draw","time","num1","num2","num3")
x<-c(1,2,3,4,5,6)
#One number
one_numb <- combn(x, 1, simplify = FALSE)
result <- list()
for(i in 1:length(one_numb)) {
result<-filter(df,(df$num1==one_numb[[i]][1] | df$num2==one_numb[[i]][1] | df$num3==one_numb[[i]][1] ))
if (nrow(result)!= 0){
cat(sprintf("\"%.0f\" \"%.0f\"\n", one_numb[i], nrow(result)))
}
}
x<-c(1,2,3,4,5,6)
#Two numbers
two_numb <- combn(x, 2, simplify = FALSE)
result <- list()
for(i in 1:length(two_numb)) {
result<-filter(df,(df$num1==two_numb[[i]][1] | df$num2==two_numb[[i]][1]
| df$num3==two_numb[[i]][1])
&(df$num1==two_numb[[i]][2] | df$num2==two_numb[[i]][2]
| df$num3==two_numb[[i]][2] ) )
if (nrow(result)!= 0){
cat(sprintf("\"%.0f\" \"%.0f\"\n", two_numb[i], nrow(result)))
}
}
解决方案
推荐阅读
- language-agnostic - (x & (x >> 1) == 0) 检查什么?
- perl - 如何在模板工具包 MACRO 中定义默认参数值
- ms-access - Access 中的 InputBox 即使在输入信息后仍不断要求提示
- export - EEGLAB 无法导出 .edf 文件
- node.js - 检查 NodeJS WriteStream 是否仍然有效
- reactjs - Memoize 自定义钩子的返回值
- javascript - 在 React/JavaScript 中为二维数组赋值
- ecmascript-6 - 未捕获的类型错误:代理上的“ownKeys”:陷阱结果不包括“长度”
- python - 将 `runpy` 与 mock 组合在单独运行时通过,但在与其他测试一起运行时失败
- python - 为什么我不能在 pyqt5 gui 中嵌入的 matplotlib 颜色图中成功绘制感兴趣区域?