繁体   English   中英

r打印频率高于某个值

[英]r print frequencies above a value

如何在具有行/列名称的表格中打印有效频率?

with(mtcars,table(cyl,carb))

   carb
cyl 1 2 3 4 6 8
  4 5 6 0 0 0 0
  6 2 0 0 4 1 0
  8 0 4 3 6 0 1

我想查看频率为5以上的行和列

   carb
cyl 1 2 4
  4 5 6 0
  8 0 4 6

或者,关于如何查看具有100行200列的频率表中的重要数据的任何建议。

可以打印以下内容吗?

cyl  carb  count
4     1      5
4     2      5
8     4      6

您可以尝试:

tbl <- with(mtcars, table(cyl, carb))
dat1 <- subset(as.data.frame(with(mtcars,table(cyl,carb))), Freq>=5) 
tbl2 <- xtabs(Freq~., droplevels(dat1))
indx <- match(outer(rownames(tbl2), colnames(tbl2), FUN=paste0),outer(rownames(tbl), colnames(tbl), FUN=paste0))

 tbl2[] <- tbl[indx]
 tbl2
 #  carb
#cyl  1 2 4
#   4 5 6 0
#   8 0 4 6

要么

  indx <- tbl>=5
  tbl[!!rowSums(indx), !!colSums(indx)]
    carb
 #cyl 1 2 4
 #  4 5 6 0
 #  8 0 4 6

关于第一个问题,你可以使用arr.ind的说法which获得您要选择的行和列:

x <- with(mtcars,table(cyl,carb))
inds <- which(x>=5,arr.ind=TRUE)

x[unique(inds[,"row"]),unique(inds[,"col"])]
   carb
cyl 1 2 4
  4 5 6 0
  8 0 4 6

第二个问题比较容易,只需强制转换为data.framesubset

subset(as.data.frame(x),Freq>=5)
   cyl carb Freq
1    4    1    5
4    4    2    6
12   8    4    6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM