While looking at an answer posted recently on SO, I noticed an unfamiliar assignment statement. Instead of the usual form of myVar<- myValue
, it used the for myVar[]<- myValue
. Personally, I had never seen such an assignment, but it had a highly useful effect-- it reshaped the assigned data myValue to the shape of myVar.
I would like to use this in my code. However the documentation for "<-"
seems to be silent on it.
Is this a well established feature and one can rely on it to work in all cases?
Also, my guess is that it might be a side effect of a function call stack, i.e. calling <-
and [
in sequence, but I could not figure out how. Can someone throw some light on that?
Here's an example--
# A dataframe
df1<- data.frame(a=1:4, b=11:14)
# simple assignment assigns to class of RHS
df1<- c(21:24,31:34)
df1
#[1] 21 22 23 24 31 32 33 34
class(df1)
#[1] "integer"
#assignment with [] casts to class of LHS
df1<- data.frame(a=1:4, b=11:14)
df1[]<- c(21:24,31:34)
df1
# a b
# 1 21 31
# 2 22 32
# 3 23 33
# 4 24 34
# recycling to preserve shape
df1[]<- c(101:102)
df1
# a b
# 1 101 101
# 2 102 102
# 3 101 101
# 4 102 102
class(df1)
#data.frame
# reshaping
df1<- data.frame(a=1:4, b=11:14)
df1[] <- matrix(1:8, 2,4)
df1 #matrix reshaped
class(df1)
#[1] "data.frame"
# flattening
x<- 1:8
x[]<- matrix(1:8,4,2)
x
#[1] 1 2 3 4 5 6 7 8