从命名向量中删除名称,仅获取值 [英] Remove names from named vector and get only the values

查看:63
本文介绍了从命名向量中删除名称,仅获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面的向量

tmp <- c(a=1, b=2, c=3)

a b c
1 2 3

a b c
1 2 3

我想展平这个向量,只得到1, 2, 3.

I want to flatten this vector to get only 1, 2, 3.

我尝试了unlist(tmp),但是它仍然给我相同的结果.

I tried unlist(tmp) but it still gives me the same result.

如何有效地实现这一目标?

How to achieve that efficiently?

推荐答案

您只想从tmp中删除names属性.有很多方法可以做到这一点.

You just want to remove the names attribute from tmp. There are a number of ways to do that.

您可以unname它.

unname(tmp)
# [1] 1 2 3

或者通过将名称设置为NULL来使用一种非常常用的方法来删除名称.

Or use a very common method for removing names, by setting them to NULL.

names(tmp) <- NULL

或使用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

或使用setNames.

setNames(tmp, NULL)
# [1] 1 2 3

这篇关于从命名向量中删除名称,仅获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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