Extract a row from a table object

后端 未结 1 1800

I want to know how to get a specific row from a table in R. For example,

> a <- c(13,13, 
    14,14,14,14,14,14,
    15,15,15,15,15,15,
    16,16,16,16         


        
1条回答
  •  梦毁少年i
    2020-12-24 02:50

    The function str() lets you interrogate the structure of an object

    str(table(a))
    # 'table' int [1:14(1d)] 2 6 6 15 7 17 11 9 11 9 ...
    # - attr(*, "dimnames")=List of 1
    #  ..$ a: chr [1:14] "13" "14" "15" "16" ...
    

    Your table object is similar to a vector (it just has some additional tags/attributes). Crucially, you can access the elements in the usual way:

    R> b = table(a)
    ##To get the numerical values
    R> as.vector(b)
     [1]  2  6  6 15  7 17 11  9 11  9  3  1  2  1
    ##To get the names
    R> names(b)
     [1] "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "27"
    

    Also, we can use standard sub-setting rules

    ##Get the last element in the named vector
    R> b[length(b)]
    27 
     1 
    R> names(b)[length(b)]
    [1] "27"
    

    0 讨论(0)
提交回复
热议问题