[英]r print frequencies above a value
How do I print the significant frequencies in a table, with row / column names? 如何在具有行/列名称的表格中打印有效频率?
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
I'd like to see the rows and columns for frequencies 5 and above 我想查看频率为5以上的行和列
carb
cyl 1 2 4
4 5 6 0
8 0 4 6
Or, any suggestions on how to see the significant data in a frequency table with 100 rows and 200 columns. 或者,关于如何查看具有100行200列的频率表中的重要数据的任何建议。
Can the following be printed? 可以打印以下内容吗?
cyl carb count
4 1 5
4 2 5
8 4 6
You could try: 您可以尝试:
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
Or 要么
indx <- tbl>=5
tbl[!!rowSums(indx), !!colSums(indx)]
carb
#cyl 1 2 4
# 4 5 6 0
# 8 0 4 6
For your first question, you can use the arr.ind
argument of which
to get the rows and columns that you wish to select: 关于第一个问题,你可以使用
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
The second question is easier, just coerce to data.frame
and subset
: 第二个问题比较容易,只需强制转换为
data.frame
和subset
:
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.