简体   繁体   English

在R中查找表中的前n个元素

[英]Finding top n elements in a table in R

I have a data table with dim(133 x 24) I only want to see the table for which the elements are greater than a specified value. 我有一个昏暗的数据表(133 x 24)我只想看到元素大于指定值的表。

     C1 .....C24
R1  
...
...
...
R133

Supposing many of the values are <4 and I have 10 values that are greater than 5 I would like to see: 假设许多值<4且我有10个大于5的值,我希望看到:

(R1,C11) = 9
(R38,C5) = 11
(R90,C20) = 20
....

For all elements that are greater than something I specify. 对于大于我指定的所有元素。 These represent hot spots in my data set that need special attention. 这些代表了我的数据集中需要特别注意的热点。

Sample data 样本数据

 R <- c(rep("R1",20),rep("R2",5),rep("R3",25),rep("R4",9))
 C <- c(rep("C1",15),rep("C2",18),rep("C3",16),rep("C4",10))
 dt <- table(R,C)
      C
R    C1 C2 C3 C4
  R1 15  5  0  0
  R2  0  5  0  0
  R3  0  8 16  1
  R4  0  0  0  9

ind <- something(dt > 5)

R1 C1 15
R1 C2 5
R2 C2 5
R3 C2 8
R3 C3 16
R4 C4 9   

You could create a new data frame based on the attributes and values of a logical summary of dt . 您可以根据dt逻辑摘要的属性和值创建新数据框。

x <- dt >= 5
data.frame(
    row = rownames(x)[row(x)[x]],  
    col = colnames(x)[col(x)[x]],
    val = dt[x]
)
#   row col val
# 1  R1  C1  15
# 2  R1  C2   5
# 3  R2  C2   5
# 4  R3  C2   8
# 5  R3  C3  16
# 6  R4  C4   9

If you wanted to get a little fancy you could Map() over the dimnames() . 如果你想获得一点点花哨,你可以Map()dimnames()

data.frame(Map("[", dimnames(x), list(row(x)[x], col(x)[x])), val = dt[x])
#    R  C val
# 1 R1 C1  15
# 2 R1 C2   5
# 3 R2 C2   5
# 4 R3 C2   8
# 5 R3 C3  16
# 6 R4 C4   9

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

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