In Python, one can do this:
>>> a, b, c = (1, 2, 3)
>>> a
1
>>> b
2
>>> c
3
Is there a way to do it in R, as below?
> a, b, c = c(1, 2, 3)
Python tuples are immutable means that they can not be modified in whole program. Packing and Unpacking a Tuple: In Python, there is a very powerful tuple assignment feature that assigns the right-hand side of values into the left-hand side. In another way, it is called unpacking of a tuple of values into a variable.
Sequence unpacking in python allows you to take objects in a collection and store them in variables for later use. This is particularly useful when a function or method returns a sequence of objects.
Tuple packing refers to assigning multiple values into a tuple. Tuple unpacking refers to assigning a tuple into multiple variables.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With