简体   繁体   中英

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.

     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:

(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 .

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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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