Python-like unpacking of numeric value in R [duplicate]

大憨熊 提交于 2019-12-03 12:26:22

You can do this within a list using [<-

e <- list()

e[c('a','b','c')] <- list(1,2,3)

Or within a data.table using :=

library(data.table)
DT <- data.table()
DT[, c('a','b','c') := list(1,2,3)]

With both of these (lists), you could then use list2env to copy to the global (or some other) environment

list2env(e, envir = parent.frame())

a
## 1
b
## 2
c
## 3

But not in general usage creating objects in an environment.

maybe it looks stupid, but I would do this :

v <- list(a=0,b=0,c=0)
v[] <- c(1,2,3)
 v
$a
[1] 1

$b
[1] 2

$c
[1] 3
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!