将所选变量输出到全局环境R函数 [英] Output selected variables to global environment R function

查看:43
本文介绍了将所选变量输出到全局环境R函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有的功能是这里先前问题的扩展

I have function which is an extension of an earlier question here

功能计算按列计算的R数据帧的中位数,定期对多个数据帧进行计算

我下面的功能

library(outliers)
MscoreMax <- 3

scores_na <- function(x, ...) {
  not_na <- !is.na(x)
  scores <- rep(NA, length(x))
  scores[not_na] <- outliers::scores(na.omit(x), ...)
  scores
}


mediansFunction <- function(x){
  labmedians <- sapply(x[-1], median)
  median_of_median <- median(labmedians)
  grand_median <- median(as.matrix(x[-1]))
  labMscore <- as.vector(round(abs(scores_na(labmedians, "mad")), digits = 2)) #calculate mscore by lab
  labMscoreIndex <- which(labMscore > MscoreMax) #get the position in the vector that exceeds Mscoremax
  x[-1][labMscoreIndex] <- NA # discharge values above threshold by making NA
  
     
       return(x) 
}


该函数具有将超出阈值的我的Mscore值转换为NA的预期结果.但是,我想发送

the function has the desired outcome of converting my Mscore values above the threshold to NA. However, I would like to send

  • 实验室中位数
  • grand_median
  • labMscore

作为自变量从函数内部移至全局环境,但不作为3个变量的项列表.我可以这样做还是最好创建一个稍有不同的第二个函数,该函数将变量作为函数发送到全局环境,然后在函数外使用list2env提取变量作为单独的项目?

As their own variables to the global environment from within the function, but not as a list of items as 3 variables. Can i do this or is better to create a second function which is slightly different that sends the variables to the global environment as a function then use list2env outside the function afterwards to extract the variables as seperate items?

我的df以下

structure(list(Determination_No = 1:6, `2` = c(0.08, 0.08, 0.08, 
0.08, 0.08, 0.08), `3` = c(0.08, 0.07, 0.07, 0.08, 0.07, 0.07
), `4` = c(0.07, 0.08, 0.08, 0.08, 0.07, 0.08), `5` = c(0.08, 
0.08, 0.08, 0.08, 0.09, 0.09), `7` = c(0.09, 0.09, 0.11, 0.1, 
0.1, 0.1), `8` = c(0.086, 0.087, 0.086, 0.09, 0.083, 0.079), 
    `10` = c(0.049748274, 0.049748274, 0.066331032, 0.066331032, 
    0.066331032, 0.049748274), `12` = c(0.086, 0.078, 0.078, 
    0.077, 0.077, 0.068)), class = "data.frame", row.names = c(NA, 
-6L))


推荐答案

不建议从函数内部写入全局环境.如果要在全局环境中创建多个对象,请从该函数返回一个命名列表,并使用 list2env .

It is not recommended to write to global environment from inside the function. If you want to create multiple objects in the global environment return a named list from the function and use list2env.

mediansFunction <- function(x){
  labmedians <- sapply(x[-1], median)
  median_of_median <- median(labmedians)
  grand_median <- median(as.matrix(x[-1]))
  labMscore <- as.vector(round(abs(scores_na(labmedians, "mad")), digits = 2)) #calculate mscore by lab
  labMscoreIndex <- which(labMscore > MscoreMax) #get the position in the vector that exceeds Mscoremax
  x[-1][labMscoreIndex] <- NA # discharge values above threshold by making NA
  dplyr::lst(data = x, labmedians, grand_median, labMscore)
}

result <- mediansFunction(df)
list2env(result, .GlobalEnv)

现在,您在全局环境中具有变量 data labmedians grand_median labMscore .

Now you have variables data, labmedians, grand_median and labMscore in the global environment.

这篇关于将所选变量输出到全局环境R函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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