How to use a value that is specified in a function

2019-08-17 01:40发布

问题:

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.

回答1:

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.



回答2:

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:

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.