User7598
User7598

Reputation: 1678

Count values in table R

If I have a table, out.i I'd like R to return the column name for cells that exceed a certain value (e.g., .5). If no cell exceeds that value, I'd like to be able to return an NA.

For example, given out.i

> dput(out.i)
out.i=structure(c(0, 0, 0, 1, 0, 1, 0, 0.5, 1, 0, 1, 0, 0, 0, 0, 0, 
0, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 
0, 0, 0.5, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0.5, 0, 0, 0, 0, 1, 
0), class = "table", .Dim = c(11L, 5L), .Dimnames = structure(list(
    c("0:36", "1:14", "1:32", "10:00", "2:10", "3:23", "4:37", 
    "5:30", "7:03", "7:34", "8:52"), c("4", "6", "7", "8", "10")), .Names = c("", "")))

I'd like to return the row and column names for all values over .5.

This should yield:

0:36  10
1:14  10
1:32  10
10:00 4
2:10  NA

Upvotes: 0

Views: 441

Answers (2)

SabDeM
SabDeM

Reputation: 7190

My output does not fit exactly yours, Maybe I don't get the point here, anyway here is a dplyr solution with the very useful add_rownames function.

library(dplyr)
as.data.frame(which(out.i > 0.5, arr.ind = T)) %>% add_rownames()
Source: local data frame [8 x 3]

  rowname row col
1   10:00   4   1
2    3:23   6   1
3    7:03   9   1
4    8:52  11   1
5    0:36   1   5
6    1:14   2   5
7    1:32   3   5
8    7:34  10   5

Upvotes: 1

josliber
josliber

Reputation: 44309

You can use which with arr.ind=TRUE:

indices <- which(out.i > 0.5, arr.ind=TRUE)
data.frame(row.name=rownames(out.i)[indices[,1]],
           col.name=colnames(out.i)[indices[,2]])
  row.name col.name
1    10:00        4
2     3:23        4
3     7:03        4
4     8:52        4
5     0:36       10
6     1:14       10
7     1:32       10
8     7:34       10

This returns all cells in the matrix that exceed 0.5. If you instead wanted just the first element in each row exceeding the value, with NA for rows with no such elements, you could try:

data.frame(row.name=rownames(out.i),
           col.name=colnames(out.i)[apply(out.i, 1, function(x) head(c(which(x > 0.5), NA), 1))])
#    row.name col.name
# 1      0:36       10
# 2      1:14       10
# 3      1:32       10
# 4     10:00        4
# 5      2:10     <NA>
# 6      3:23        4
# 7      4:37     <NA>
# 8      5:30     <NA>
# 9      7:03        4
# 10     7:34       10
# 11     8:52        4

Upvotes: 6

Related Questions