简体   繁体   English

如何根据 function arguments 重命名 R 中的对象?

[英]How to rename objects in R based on function arguments?

Let's say I have a function which returns a vector based on some dataframe argument.假设我有一个 function ,它返回一个基于一些 dataframe 参数的向量。 I want to use this function iteratively in a loop, so I want the vectors it returns to have different names (otherwise I would not be able to distinguish them).我想在循环中迭代地使用这个 function,所以我希望它返回的向量具有不同的名称(否则我将无法区分它们)。 Specifically, the name of the returned vector needs to change based on some index, which I pass to the function as an argument, t.具体来说,返回向量的名称需要根据某个索引进行更改,我将其作为参数 t 传递给 function。 So, if I call the function with the argument t=1, it returns a vector called (for example) "vector1";因此,如果我使用参数 t=1 调用 function,它会返回一个名为(例如)“vector1”的向量; if I call the function with the argument t=2, it returns a vector called "vector2," and so on.如果我用参数 t=2 调用 function,它会返回一个名为“vector2”的向量,依此类推。 Note that for the purposes of this question it doesn't really matter what the elements of the vectors are;请注意,就这个问题而言,向量的元素是什么并不重要。 they can be all the same or all different--I just want to rename them based on an argument to the function.它们可以完全相同或完全不同——我只想根据 function 的参数重命名它们。

I'm new to R so sorry if this question is asked inappropriately.我是 R 的新手,如果这个问题被问得不恰当,我很抱歉。 Below is what I have tried, which is obviously wrong, but hopefully illustrates what I am trying to do.以下是我尝试过的,这显然是错误的,但希望能说明我正在尝试做的事情。

#this function is passed a dataframe and returns some vector named ["vector" + the argument t] 

indexed_vector <- function(data, t) { 

   # ... code which creates a vector (called "vector") from data...#

   paste0("vector", as.character(t)) <- vector

   return(paste0("vector", as.character(t)))


}

#using the function once 
indexed_vector(data, 10)
#ideally this would return a vector called "vector10"  


#using the function in a loop to create multiple vectors
for(i in 1:10){indexed_vector(data, i))
#ideally this would create a list of vectors where each is called "vectori", 
#i.e. "vector1", "vector2", "vector3"..."vector10"

Thanks!谢谢!

In R, you can't return a vector called something.在 R 中,您不能返回名为 something 的向量。 If you want a vector called vector_1 to just appear after you call the function without having to store it anywhere, you need to use assign or the <<- operator inside the function.如果您希望在调用 function 之后只出现一个名为vector_1的向量,而不必将其存储在任何地方,则需要在 function 中使用assign<<-运算符。 This usually isn't a good idea.这通常不是一个好主意。

However, it is a good idea to have named vectors inside a list, and this can be done simply without using a loop at all:但是,在列表中包含命名向量一个好主意,这可以在根本不使用循环的情况下简单地完成:

indexed_vector <- function(df, i) setNames(as.list(df[i]), paste0("vector_", i))

df <- data.frame(a = 1:3, b = 4:6, c = 7:9, d = 10:12)

indexed_vector(df, 1:3)
#> $vector_1
#> [1] 1 2 3
#> 
#> $vector_2
#> [1] 4 5 6
#> 
#> $vector_3
#> [1] 7 8 9

Created on 2020-06-11 by the reprex package (v0.3.0)reprex package (v0.3.0) 于 2020 年 6 月 11 日创建

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

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