如果这是一个愚蠢的问题,我深表歉意 - 但这是我第一次尝试使用
R
,我最终编写了一些代码:
some <- vector('list', length(files))
thing <- vector('list', length(files))
and <- vector('list', length(files))
another <- vector('list', length(files))
thing <- vector('list', length(files))
有没有更好的(干)方法来做到这一点
R
?
换句话说,我想一次为多个变量分配相同的值(根据@Sven Hohenstein的答案)
如果您想一次为多个变量分配相同的值,请使用:
some <- thing <- and <- another <- thing <- vector('list', length(files))
作为链接多个赋值运算符(看起来很混乱)的替代方案,我建议使用
assign
函数。这是一个小示例,在所需对象名称的向量上使用 for
循环。
vars = c("some","thing","and","another","thing")
for (i in seq_along(vars)) {
assign(x = vars[i], value = "some_object")
}
ls()
#> [1] "and" "another" "i" "some" "thing" "vars"
str(some)
#> chr "some_object"