从命名向量中删除名称并仅获取值 [英] Remove names from named vector and get only the values
问题描述
我有一个像下面这样的向量
tmp <- c(a=1, b=2, c=3)
<块引用>
a b c
1 2 3
我想展平这个向量以只得到 1, 2, 3
.
我尝试了 unlist(tmp)
但它仍然给我相同的结果.
如何有效地实现这一目标?
您只想从 tmp
中删除 names
属性.有很多方法可以做到这一点.
您可以取消命名
它.
unname(tmp)# [1] 1 2 3
或者使用一种非常常见的方法来删除名称,将它们设置为NULL
.
names(tmp) <- NULL
或者使用 as.vector
去除属性.
as.vector(tmp)# [1] 1 2 3
或者在没有名称的情况下重新连接它.
c(tmp, use.names=FALSE)# [1] 1 2 3
或者使用setNames
.
setNames(tmp, NULL)# [1] 1 2 3
I have a vector like below
tmp <- c(a=1, b=2, c=3)
a b c
1 2 3
I want to flatten this vector to get only 1, 2, 3
.
I tried unlist(tmp)
but it still gives me the same result.
How to achieve that efficiently?
You just want to remove the names
attribute from tmp
. There are a number of ways to do that.
You can unname
it.
unname(tmp)
# [1] 1 2 3
Or use a very common method for removing names, by setting them to NULL
.
names(tmp) <- NULL
Or strip the attributes with as.vector
.
as.vector(tmp)
# [1] 1 2 3
Or re-concatenate it without the names.
c(tmp, use.names=FALSE)
# [1] 1 2 3
Or use setNames
.
setNames(tmp, NULL)
# [1] 1 2 3
这篇关于从命名向量中删除名称并仅获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!