如何在R的全局环境中编写函数结果 [英] How to write a result of function in the global environment in R

查看:66
本文介绍了如何在R的全局环境中编写函数结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据集:A <- 4, B <- 3, C <- 2.

所以我把它们放在列表中 D<-list(A,B,C)并想应用此功能:

So I put them in a list D<-list(A,B,C) and want to apply this function:

s<-function(x) {
    t<-8
    x<-as.data.frame(x*t)
}

lapply(D,s)

当我应用lapply函数时,它只是打印它们.

when I apply the lapply function it just print them.

如何使结果保存在全局环境中而不是打印出来?

How can I make it saving the result in the global environment instead of printing them?

因此结果应为A,值为32 B,值为24 C,值为16.

So the result should be A with value of 32 B with value of 24 C with value of 16.

推荐答案

而不是lapply(D,s),请使用:

D <- lapply(D, s)
names(D) <- c("A", "B", "C")
list2env(D, envir = .GlobalEnv)

这篇关于如何在R的全局环境中编写函数结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆