简体   繁体   English

使用列表命名对象

[英]Use a list to name objects

I have a list我有一个清单

l <- list('a','b','c')

That I want to use iteratively to create object names in a function.我想迭代地使用在函数中创建对象名称。 For example:例如:

f <- function(){
object <- 1 + 2
}

I wanted to run the operation a+b multiple times and each time create a new object with each time naming an object based on something on my list.我想多次运行 a+b 操作,每次都创建一个新对象,每次都根据我列表中的内容命名一个对象。 Is there a way to use a list to iteratively name an object?有没有办法使用列表来迭代命名对象?

I hope this question makes sense...我希望这个问题有意义...

EDIT:编辑:

I am hoping that my output would give me 3 objects: a, b, and c.我希望我的输出会给我 3 个对象:a、b 和 c。 Each of these would equal 3 based on the function.根据函数,这些中的每一个都等于 3。

Not sure what the overall goal is but here's a way to do that :不确定总体目标是什么,但这是一种方法:

f <- function(){
  object <- p + q
}
p <- 10
q <- 5

list2env(setNames(replicate(length(l), f(), simplify = FALSE), l), .GlobalEnv)

a
#[1] 15
b
#[1] 15
c
#[1] 15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM