提取R中函数内定义的对象
我正在R中编写一个函数,我希望能够从函数中调用不同的对象。我有一个简单的例子来说明我所说的问题(显然不是真正的代码)提取R中函数内定义的对象,r,function,R,Function,我正在R中编写一个函数,我希望能够从函数中调用不同的对象。我有一个简单的例子来说明我所说的问题(显然不是真正的代码) example我们可以在list中返回多个输出,然后提取list元素 example <- function(a,b){ c <- a+b d <- a*b e <- a/b list(c=c, d= d, e = e) } a <- 10 b <- 20 output <- example(a,b)[['c'
example我们可以在list
中返回多个输出,然后提取list
元素
example <- function(a,b){
c <- a+b
d <- a*b
e <- a/b
list(c=c, d= d, e = e)
}
a <- 10
b <- 20
output <- example(a,b)[['c']]
output
#[1] 30
example(a,b)[['d']]
#[1] 200
非常感谢您的示例,非常有效。如果你能提供更多关于函数如何设置要从函数中输出的对象的见解,我会很感兴趣。
example <- function(a,b){
c <- a+b
d <- a*b
e <- a/b
list(c=c, d= d, e = e)
}
a <- 10
b <- 20
output <- example(a,b)[['c']]
output
#[1] 30
example(a,b)[['d']]
#[1] 200