我可以用v <- numeric(0)
创建一个空矢量,但是矢量没有命名,只有类型num
。当我评估names(v) <- character(0)
时,我有一个空的命名数字向量:
> v <- numeric(0)
> names(v) <- character(0)
> str(v)
Named num(0)
- attr(*, "names")= chr(0)
> v["test"] <- 1
> str(v)
Named num 1
- attr(*, "names")= chr "test"
是否有更简单的方法来创建空的命名数字向量?似乎没有像named(0)
这样的构造函数,或者我是否想念它?
答案 0 :(得分:-1)
可以从Create a numeric vector with names in one statement?派生的答案是:
structure()
。setNames()
。例如:
> v <- structure(numeric(0), names=character(0))
> str(v)
Named num(0)
- attr(*, "names")= chr(0)
> v <-setNames(numeric(0), character(0))
> str(v)
Named num(0)
- attr(*, "names")= chr(0)