9

I have a frequency table, counting the frequency of elements in a vector

a = table(c(0,1,1,1,0,2,2,4,1,2,3,2,1,2,3,1,1,1,2,3,4,1,1,0))
a
#  0  1  2  3  4 
#  3 10  6  3  2

I know I can access the name by names(a). But when I tried to access the values of the SECOND row

a[1, "0"]
# Error in a[1, "0"] : incorrect number of dimensions

a[1, 1]
# Error in a[1, 1] : incorrect number of dimensions
IRTFM
  • 258,963
  • 21
  • 364
  • 487
Minh Nguyen
  • 111
  • 1
  • 4
  • 1
    I'd strongly recommend avoiding using `table` altogether and using `dplyr`: `x %>% tally()`. This returns a table in which these sort of operations are easy. – Hugh Nov 27 '14 at 02:35

4 Answers4

13

This table is actually an array.

x <- c(0,1,1,1,0,2,2,4,1,2,3,2,1,2,3,1,1,1,2,3,4,1,1,0)
(a <- table(x))
#
# 0  1  2  3  4 
# 3 10  6  3  2 
class(unclass(a))
# [1] "array"

Its names are on top, and values on bottom.

names(a)
[1] "0" "1" "2" "3" "4"

You can access its elements a number of ways.

a[1]                     ## access named element by index
# 0 
# 3
a[[1]]                   ## access unnamed element by index
# [1] 3
a["0"]                   ## access named element by name
# 0 
# 3 
a[["0"]]                 ## access unnamed element by name
# [1] 3
as.vector(a)             ## as.vector() drops table down to unnamed vector
# [1]  3 10  6  3  2
c(a)[2:4]                ## c() drops table down to named vector
#  1  2  3 
# 10  6  3 
class(a[2:4])
# [1] "array"
class(c(a)[2:4])
# [1] "integer"

It also has an nrow and dim attribute, which are set up in the last few lines of table.

y <- array(tabulate(bin, pd), dims, dimnames = dn)
class(y) <- "table"

Although it's really not clear to me why nrow(a) is 5 but a[1,] returns an error.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
  • I wanted to put this explanation, but if you do `nrow(a)`, you actually get a result (though not what you would think). – A5C1D2H2I1M1N2O1R2T1 Nov 27 '14 at 02:08
  • Looks good. Don't know why you've put `c(a)[2:4]` though - just `a[2:4]` gives nearly the same result, just without dimensions. – thelatemail Nov 27 '14 at 02:29
  • 1
    The reason `a[1, ]` returns an error is that the `a`-table only has one dimension and that code is trying access it with two. – IRTFM Sep 03 '15 at 16:55
4

The table() command is returning a named vector, not a matrix or data.frame. If you want to access the count of zeros, you'd do

a["0"]

Note that the numeric properties of the levels are lost because the names of named vectors must be characters. You can convert them back with as.numeric(names(a)) if you like

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This is correct. Try a['0']==3. That will show you that the names of the vectors are 0,1,2,3,4. – Jason Nov 27 '14 at 02:25
4

a = as.numeric(names(a))

it will give you access to the first column

ali saadat
  • 41
  • 1
2

The table you are using is a one dimensional array and you are using 2- dimensions to retrieve an element that's wwhy you are getting that error.

Use something like a[1]

Suri
  • 66
  • 6