在查看最近在 SO 上发布的答案时,我注意到一个不熟悉的赋值语句。而不是 myVar<- myValue
的通常形式,它使用了 myVar[]<- myValue
的形式,即左侧的对象用空方括号索引。就我个人而言,我从未见过这样的分配,但它有一个非常有用的效果——它将分配的数据“myValue” reshape 为“myVar”的形状。
我想在我的代码中使用它,因为这会让事情变得更容易。但是 "<-"
的文档似乎对此保持沉默。
这是一项完善的功能,并且可以在所有情况下都依赖它吗?
另外,我的猜测是它可能是函数调用堆栈的副作用,即调用 <-
和 [
按顺序,但我不知道如何。有人可以对此有所了解吗?
这是一个例子——
# 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
最佳答案
这是一个有意和记录的功能。正如 joran 所提到的,文档页面“Extract”在“Atomic Vectors”部分中包含了这个:
An empty index selects all values: this is most often used to replace all the entries but keep the attributes.
但是,在递归对象的情况下(例如
data.frames
或 lists
),属性仅保留用于子集对象。它的零件没有得到这样的保护。这是一个例子:
animals <- factor(c('cat', 'dog', 'fish'))
df_factor <- data.frame(x = animals)
rownames(df_factor) <- c('meow', 'bark', 'blub')
str(df_factor)
# 'data.frame': 3 obs. of 1 variable:
# $ x: Factor w/ 3 levels "cat","dog","fish": 1 2 3
df_factor[] <- 'cat'
str(df_factor)
# 'data.frame': 3 obs. of 1 variable:
# $ x: chr "cat" "cat" "cat"
rownames(df_factor)
# [1] "meow" "bark" "blub"
df_factor
保留其rownames
属性,但 x
column 只是分配中使用的字符向量,而不是一个因子。我们可以保留x
的类(class)和级别通过专门替换其值:df_factor <- data.frame(x = animals)
df_factor$x[] <- 'cat'
str(df_factor)
# 'data.frame': 3 obs. of 1 variable:
# $ x: Factor w/ 3 levels "cat","dog","fish": 1 1 1
所以用空子集替换对于向量、矩阵和数组来说是非常安全的,因为它们的元素不能有自己的属性。但是在处理类似列表的对象时需要小心。
关于r - 分配给 LHS 上的空索引(空方括号 x[]<-),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41191623/