如何在自定义函数中重命名列表对象?

时间:2021-01-26 10:48:23
quote <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  return(list(namefoo=foo,namebar=bar))
}

From the above function, If I ran quote(test, test1) the name of the two objects in the list remain as namefoo and namebar instead of what I specified in the function call.

从上面的函数,如果我运行quote(test,test1),列表中两个对象的名称仍然是namefoo和namebar,而不是我在函数调用中指定的名称。

If I just ran the code seperately as:

如果我只是单独运行代码:

set.seed(3)
foo <- rnorm(n = 5)
bar <- rnorm(n = 5)
obj <- list(test=foo,test1=bar)

Then obj will return foo and bar with the amended names. How do I make my function do this? I've tried several combinations of including quotes as well, from the function call to the function itself but it doesn't seem to work.

然后obj将返回带有修改名称的foo和bar。如何让我的功能这样做?我已经尝试了几种包含引号的组合,从函数调用到函数本身但它似乎不起作用。

3 个解决方案

#1


2  

One way is this:

一种方法是:

quote <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  out <- list(foo, bar)
  names(out) <- c(namefoo, namebar)
  out
}

You can save the list to a variable and then name the elements with names.

您可以将列表保存到变量,然后使用名称命名元素。

# quote('foo', 'bar')
# $namefoo
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319
# [5]  0.1957828
# 
# $namebar
# [1]  0.03012394  0.08541773  1.11661021
# [4] -1.21885742  1.26736872

#2


1  

It's a very bad idea to name your function quote, a very important R function is named just like that.

命名函数引用是一个非常糟糕的主意,一个非常重要的R函数就是这样命名的。

Use setNames :

使用setNames:

fun <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  setNames(list(foo,bar),c(namefoo, namebar))
}

fun("hi","there")
# $hi
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319  0.1957828
# 
# $there
# [1]  0.03012394  0.08541773  1.11661021 -1.21885742  1.26736872

You might also see this kind of code around too, using more advanced features of rlang / tidyverse :

您也可以使用rlang / tidyverse的更高级功能看到这种代码:

library(tidyverse)
fun2 <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  lst(!!namefoo := foo,!!namebar := bar)
}

fun2("hi","there")
# $hi
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319  0.1957828
# 
# $there
# [1]  0.03012394  0.08541773  1.11661021 -1.21885742  1.26736872

#3


1  

We can do

我们可以做的

quotefn <- function(...) {
        nm <- c(...)
        out <- replicate(length(nm), rnorm(n = 5), simplify = FALSE)
        names(out) <- nm
        out}
quotefn("foo", "bar")
#$foo
#[1] -0.5784837 -0.9423007 -0.2037282 -1.6664748 -0.4844551

#$bar
#[1] -0.74107266  1.16061578  1.01206712 -0.07207847 -1.13678230

#1


2  

One way is this:

一种方法是:

quote <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  out <- list(foo, bar)
  names(out) <- c(namefoo, namebar)
  out
}

You can save the list to a variable and then name the elements with names.

您可以将列表保存到变量,然后使用名称命名元素。

# quote('foo', 'bar')
# $namefoo
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319
# [5]  0.1957828
# 
# $namebar
# [1]  0.03012394  0.08541773  1.11661021
# [4] -1.21885742  1.26736872

#2


1  

It's a very bad idea to name your function quote, a very important R function is named just like that.

命名函数引用是一个非常糟糕的主意,一个非常重要的R函数就是这样命名的。

Use setNames :

使用setNames:

fun <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  setNames(list(foo,bar),c(namefoo, namebar))
}

fun("hi","there")
# $hi
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319  0.1957828
# 
# $there
# [1]  0.03012394  0.08541773  1.11661021 -1.21885742  1.26736872

You might also see this kind of code around too, using more advanced features of rlang / tidyverse :

您也可以使用rlang / tidyverse的更高级功能看到这种代码:

library(tidyverse)
fun2 <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  lst(!!namefoo := foo,!!namebar := bar)
}

fun2("hi","there")
# $hi
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319  0.1957828
# 
# $there
# [1]  0.03012394  0.08541773  1.11661021 -1.21885742  1.26736872

#3


1  

We can do

我们可以做的

quotefn <- function(...) {
        nm <- c(...)
        out <- replicate(length(nm), rnorm(n = 5), simplify = FALSE)
        names(out) <- nm
        out}
quotefn("foo", "bar")
#$foo
#[1] -0.5784837 -0.9423007 -0.2037282 -1.6664748 -0.4844551

#$bar
#[1] -0.74107266  1.16061578  1.01206712 -0.07207847 -1.13678230