首页 > 解决方案 > 如何从命名向量中按名称删除元素?

问题描述

如何按名称从命名向量中删除元素?例如

v <- c(1, 2, 3)
names(v) <- c('a', 'b', 'c') 
# how to remove b?
v['b'] <- NULL # doesn't work
Error in v["b"] <- NULL : replacement has length zero

标签: r

解决方案


你可以使用

v[names(v) != "b"]
#a c 
#1 3 

或与setdiff

v[setdiff(names(v), "b")]

推荐阅读