从表对象中提取一行 [英] Extract a row from a table object

查看:50
本文介绍了从表对象中提取一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从 R 中的表中获取特定行.例如,

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,16,16,16,16,16,16,16,16,16,16,16,
    17,17,17,17,17,17,17,
    18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,
    19,19,19,19,19,19,19,19,19,19,19,
    20,20,20,20,20,20,20,20,20,
    21,21,21,21,21,21,21,21,21,21,21,
    22,22,22,22,22,22,22,22,22,
    23,23,23,24,25,25,27)
> table(a)
a
13 14 15 16 17 18 19 20 21 22 23 24 25 27 
2  6  6 15  7 17 11  9 11  9  3  1  2  1 

如何提取表格的最后一行?

How do I extract the last row of the table?

推荐答案

str() 函数让你查询一个对象的结构

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

你的table 对象类似于一个向量(它只是有一些额外的标签/属性).至关重要的是,您可以以通常的方式访问元素:

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"

这篇关于从表对象中提取一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆