首页 > 解决方案 > 在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)))
}
}

标签: r

解决方案


推荐阅读