3

I searched a lot but did not find any answer to my question, even though I am sure it should not be so difficult. The thread that came closest received no answers (How do I access the name of the variable assigned to the result of a function within the function in R?)

In any case, I am trying to do the following: the function should create two objects, z and doc, and return it using the assigned name, and not the variable name. A short example:

fun.docmerge <- function(x, y, z, crit, typ, doc = checkmerge) {
  mergedat <- paste(deparse(substitute(x)), "+",
                    deparse(substitute(y)), "=", z)
  countdat <- nrow(x)
  check_t1 <- data.frame(mergedat, countdat)
  z <- join(x, y, by = crit, type = typ)
  countdat <- nrow(z)
  check_t2 <- data.frame(mergedat, countdat)
  doc <- rbind(doc, check_t1, check_t2)
  return(list(checkmerge = doc, z = z))
}

results <- fun.docmerge(x = df1, y = df2, z = "df3", crit = c("id"), typ = "left")

Some sample data:

df1 <- structure(list(id = c("XXX1", "XXX2", "XXX3", 
"XXX4"), tr.isincode = c("ISIN1", "ISIN2", 
"ISIN3", "ISIN4")), .Names = c("id", "isin"
), row.names = c(NA, 4L), class = "data.frame")

df2 <- structure(list(id= c("XXX1", "XXX5"), wrong= c(1L, 
1L)), .Names = c("id", "wrong"), row.names = 1:2, class = "data.frame")

checkmerge <- structure(list(mergedat = structure(integer(0), .Label = character(0), class = "factor"), 
    countdat = numeric(0)), .Names = c("mergedat", "countdat"
), row.names = integer(0), class = "data.frame")

The problem is that this returns z as z. However, I want it to be returned as df3 (the name assigned as argument). Is there a way to do that? I could easily solve it to return doc as checkmerge. However, z is dynamic so this would not work.

7
  • Can you provide an example of df1 and df2 for the example? Commented Nov 9, 2017 at 12:40
  • I added a reprod example Commented Nov 9, 2017 at 12:51
  • Just a hint: You are doing unusual things and I don't find your goal very useful. However, if you want to document joins that lead to a specific data.frame, I'd suggest storing that information in an attribute of the data.frame. Commented Nov 9, 2017 at 13:08
  • 1
    help("attr") is really all you need to read. Commented Nov 9, 2017 at 13:14
  • 1
    I have no idea what you are trying to do and in particular why you believe to need this. To me it appears that you can get all information by simply reading (and if necessary commenting) your script. Commented Nov 9, 2017 at 14:26

1 Answer 1

3

Try this

fun.docmerge <- function(x, y, z, crit, typ, doc = checkmerge) {
  mergedat <- paste(deparse(substitute(x)), "+",
                    deparse(substitute(y)), "=", z)
  countdat <- nrow(x)
  check_t1 <- data.frame(mergedat, countdat)
  z1 <- join(x, y, by = crit, type = typ)
  countdat <- nrow(z1)
  check_t2 <- data.frame(mergedat, countdat)
  doc <- rbind(doc, check_t1, check_t2)
  t1<-list()
  t1[["checkmerge"]]<-doc
  t1[[z]]<-z1
  return(t1)
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! But when calling this function via results <- fun.docmerge(x = df1, y = df2, z = "df3", crit = c("id"), typ = "left") I get Error in temp[[checkmerge]] <- doc : invalid subscript type 'list'
updated. if checkmerge is not a variable , then simply put that in quotes. May be if you can give a working set , we can help out. What i understood from your question is that , you want to put the variable value as the variable name . In order to do that t[[x]]<-x ( is like t$"temp1" <- temp1 ( where x<-"temp1")
Thanks, but it is still the same. I added sample data into the question, if you define the function, define the data and evaluate the function as in my comment you get the error message, also with the updated function.
Is it join or merge? which packages are you using , i am getting error trying to load it
Check now the updated code, but your requirements seems to be strange :). Anyway i will leave it to you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.