gpt4 book ai didi

r - 从表对象中提取一行

转载 作者:行者123 更新时间:2023-12-03 11:35:36 25 4
gpt4 key购买 nike

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

> 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

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

最佳答案

函数str()允许您查询对象的结构

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

您的表对象类似于向量(它只是有一些额外的标签/属性)。至关重要的是,您可以以通常的方式访问元素:
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"

此外,我们可以使用标准的子设置规则
##Get the last element in the named vector
R> b[length(b)]
27
1
R> names(b)[length(b)]
[1] "27"

关于r - 从表对象中提取一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10104400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com