如何使用函数调用中指定的值作为“变量” [英] How to use a value that is specified in a function call as a "variable"

查看:114
本文介绍了如何使用函数调用中指定的值作为“变量”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在R中是否可以使用在函数调用中声明的值作为函数本身的变量部分,类似于SAS IML中提供的功能。



给出类似这样的内容:

  put.together<  - 函数(后缀,数字){
new.suffix<< - as.data.frame(数字)
return(new.suffix)
}

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

new.part.a #####不存在! !

new.suffix ##### does exists

因为它是写,该函数返回一个名为 new.suffix 的数据框,因为它应该是我要求它做的。



我想要得到一个名为 new.part.a 的返回数据框。




编辑:请求了关于分析目的的其他信息



问题的目的是产生将发送到另一个函数进行分析的数据帧。

存在一个数据库,其中元素按数字分组,其他人将组
组织为一个有意义的集合。

每个组都有一个id号码。我使用其他人提供的信息将它们组合在一起。



例如,我会得到一组id值,如:part-1 =因此,第1部分有7个组,每个组有几个元素。



我在合并中使用id号,以便只从大数据库中提取感兴趣的组。

以下是我对一套产品的要求:

  ### all.possible.elements.bank<  - .csv文件来自大型数据库### 

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))

上面的过程重复了很多次。



我知道我可以为所有集合进行这样的操作,但我认为我可以将选择过程包装到一个函数中。唯一可能改变的是零件号码(第一部分,第二部分等)以及被选出的组。

解决方案

可以使用 assign 函数(可能还有 deparse 替代),但强烈建议不要这样做。为什么不能直接返回数据框并调用函数:

  new.part.a<  -  put。一起(x)

这是一般较好的方法。

如果您确实想在全局环境中更改内容,那么您可能需要一个宏,请参阅 defmacro 函数在 gtools 包中,最重要的是阅读帮助页面refrences部分的文档。


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.

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.

I would like to get a dataframe returned that is called 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. 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.

So, part-1 has seven groups, each group having several elements.

I use the id numbers in a merge so that only the groups of interest are extracted from the large data bank.

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.

解决方案

It is possible using the assign function (and possibly deparse and substitute), but it is strongly discouraged to do things like this. 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.

这篇关于如何使用函数调用中指定的值作为“变量”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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