convert a vector to a list

后端 未结 4 1077
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 16:03

I have a vector like this

 c(\"1\", \"a\",\"b\")

and I\'d like to create this list

list(\"a\"=1,\"b\"=1)

is

4条回答
  •  无人及你
    2021-02-15 16:59

    Using as.list and setNames:

    x = c("1", "a","b")
    as.list(setNames(rep(as.numeric(x[1]), length(x) - 1), x[-1]))
    

提交回复
热议问题