简体   繁体   English

如何使用在函数调用中指定为“变量”的值

[英]How to use a value that is specified in a function call as a “variable”

I am wondering if it is possible in R to use a value that is declared in a function call as a "variable" part of the function itself, similar to the functionality that is available in SAS IML. 我想知道R中是否可以使用在函数调用中声明的值作为函数本身的“可变”部分,类似于SAS IML中可用的功能。

Given something like this: 给定这样的东西:

put.together <- function(suffix, numbers) {
new.suffix <<- as.data.frame(numbers)
return(new.suffix)
}

x <- c(seq(1000,1012, 1))
put.together(part.a, x)

new.part.a   ##### does not exist!!

new.suffix   ##### does exist

As it is written, the function returns a dataframe called new.suffix , as it should because that is what I'm asking it to do. 在编写该函数时,该函数应返回一个名为new.suffix的数据帧,因为这就是我要它执行的操作。

I would like to get a dataframe returned that is called new.part.a . 我想返回一个称为new.part.a的数据new.part.a


EDIT: Additional information was requested regarding the purpose of the analysis 编辑:请求有关分析目的的其他信息

The purpose of the question is to produce dataframes that will be sent to another function for analysis. 这个问题的目的是产生将发送到另一个函数进行分析的数据帧。

There exists a data bank where elements are organized into groups by number, and other people organize the groups into a meaningful set. 有一个数据库,其中按数字将元素分组,而其他人则将这些分组组织成有意义的集合。

Each group has an id number. 每个组都有一个ID号。 I use the information supplied by others to put the groups together as they are specified. 我使用其他人提供的信息将这些组按照指定的方式放在一起。

For example, I would be given a set of id numbers like: part-1 = 102263, 102338, 202236, 302342, 902273, 102337, 402233. 例如,我将获得一组ID号,例如:part-1 = 102263、102338、202236、302342、902273、102337、402233。

So, part-1 has seven groups, each group having several elements. 因此,第1部分有七个组,每个组包含几个元素。

I use the id numbers in a merge so that only the groups of interest are extracted from the large data bank. 我在合并中使用ID号,以便仅从大型数据库中提取感兴趣的组。

The following is what I have for one set: 以下是我的一套装备:

### all.possible.elements.bank <- .csv file from large database ###

id.part.1 <- as.data.frame(c(102263, 102338, 202236, 302342, 902273, 102337, 402233))
bank.names <- c("bank.id")
colnames(id.part.1) <- bank.names
part.sort <- matrix(seq(1,nrow(id.part.1),1)) 
sort.part.1 <- cbind(id.part.1, part.sort)

final.part.1 <- as.data.frame(merge(sort.part.1, all.possible.elements.bank, 
by="bank.id", all.x=TRUE))

The process above is repeated many, many times. 重复上述过程很多次。

I know that I could do this for all of the collections that I would pull together, but I thought I would be able to wrap the selection process into a function. 我知道我可以对所有要收集在一起的集合执行此操作,但是我认为我可以将选择过程包装到一个函数中。 The only things that would change would be the part numbers (part-1, part-2, etc..) and the groups that are selected out. 唯一会改变的是零件编号(零件1,零件2等)和选定的组。

It is possible using the assign function (and possibly deparse and substitute ), but it is strongly discouraged to do things like this. 这是可能使用assign功能(也可能是deparsesubstitute ),但我们强烈不鼓励做这样的事情。 Why can't you just return the data frame and call the function like: 为什么不能只返回数据帧并像下面这样调用函数:

new.part.a <- put.together(x)

Which is the generally better approach. 这通常是更好的方法。

If you really want to change things in the global environment then you may want a macro, see the defmacro function in the gtools package and most importantly read the document in the refrences section on the help page. 如果您确实想在全局环境中进行更改,则可能需要一个宏,请参阅gtools软件包中的defmacro函数,最重要的是阅读帮助页面上“参考”部分中的文档。

This is rarely something you should want to do... assigning to things out of the function environment can get you into all sorts of trouble. 这很少是您应该要做的事情...在功能环境之外分配东西会导致您遇到各种麻烦。

However, you can do it using assign : 但是,您可以使用assign来做到这一点:

put.together <- function(suffix, numbers) {
   assign(paste('new',
                deparse(substitute(suffix)),
                sep='.'),
          as.data.frame(numbers),
          envir=parent.env(environment()))
}

put.together(part.a, 1:20)

But like Greg said, its usually not necessary, and always dangerous if used incorrectly. 但是就像格雷格所说的那样,它通常不是必须的,如果使用不当,总是很危险的。

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

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