3

I am trying to concatenate my function's arguments inside the body of my function for further evaluation within the bode. I went through many code improvements trying to solve the issue but couldn't.

Example: When providing a function call like fun(x,y), I want to be able to paste the characters I entered for x, to $ and the characters I entered for y to call a field within a data set for analysis. So if I provided the characters car for argument x, and toyota for argument y, I would get car$toyota variable name to use within the body of the function.

I tried:

gtData <- function(data,field,k)
d <- diff(data$field)  ## I also tried sum(data$field) to eliminate issues with diff()

but d evaluated to 0 ## I know it's not because when I run the code diff(car$toyota) I get the right answer. I don't think its doing what I want it to do. I also tried to paste the arguments within the body like this:

gtData(data,field,k)
a <- paste(data,"$",field)

It complained toyota doesn't exist because toyota is not a data set but a field in a data set.

I tried many other variation, it seems paste() can't do what I want it to do here.

What I am trying to get is the string car$toyota so I could pass it further down the body as a variable for further evaluation.

0

2 Answers 2

8

It is good to remember fortune(312)

The problem here is that the $ notation is a magical shortcut and like any other magic if used incorrectly
is likely to do the programmatic equivalent of turning yourself into a toad.
   -- Greg Snow (in response to a user that wanted to access a column whose name is stored in y via x$y
      rather than x[[y]])
      R-help (February 2012)

What you want is [[ not $

gtData <- function(data,field,k){
  a <- data[[field]]
}

Paste is definitely the wrong thing here.

Edit -- partial matching

If you wanted to allow partial matching, you can set the exact for [[ to FALSE or NA. From ?`[[`

Value NA allows partial matching but issues a warning when it occurs. Value FALSE allows partial matching without any warning.

and again

x$name is equivalent to x[["name", exact = FALSE]].


In light of your comments. I think you need to look at the R manuals 2.1.8 and Hadley's devtools wiki at how function arguments and promises

Promise objects are part of R's lazy evaluation mechanism. They contain three slots: a value, an expression, and an environment. When a function is called the arguments are matched and then each of the formal arguments is bound to a promise. The expression that was given for that formal argument and a pointer to the environment the function was called from are stored in the promise.

Until that argument is accessed there is no value associated with the promise. When the argument is accessed, the stored expression is evaluated in the stored environment, and the result is returned. The result is also saved by the promise. The substitute function will extract the content of the expression slot. This allows the programmer to access either the value or the expression associated with the promise.

So when you pass

gtData(data = mtcars, field = mpg)

the argument data is a promise, which will have the value being the mtcars object from the calling environment when it is accessed within the function.

If, for field you want to access the character string "mpg" not the value associated with mpg in the calling environment, as suggested in the manual, the idiomatic way to do this is to use deparse(substitute())

eg

gtData <- function(data,field,k){
  fieldChar <- deparse(substitute(field))
  a <- data[[fieldChar]]
}

The danger doing this will be if you were to then think you could access field within the function, in which case it would be evaluated, and will give an error (if it doesn't exist in the calling environment), or a perhaps unintended value if it were defined

9
  • I can see car[["Toyota"]] works to name the field outside the body of a formula. But when I try it inside the body it complains that the field in this case "Toyota" is not a known object. Feb 8, 2013 at 1:49
  • When you pass the argument data is it gtData(data = car,'Toyota') or data = 'car'
    – mnel
    Feb 8, 2013 at 1:50
  • Sorry, I ran out of time before finishing my comment, but here it is:I can see car[["Toyota"]] works to call a field outside the body of a function. But when I try it inside the body it complains that the field in this case "Toyota" is not a known object. I tried it with this simple code applying the sum function. gtData <- function(dd,ff){ a <- dd[["ff"]] a <- sum(a) return(a)} but it complained the field is not a known object. I tried it without Feb 8, 2013 at 1:57
  • a <- dd[["ff"]] should be a <- dd[[ff]] , as my answer states. The extract from ?'[[' with "name" is just emphasising that the name of the column is a character string (which your argument named field contains)
    – mnel
    Feb 8, 2013 at 2:00
  • when I call the function I am just passing car , Toyota as argument the exact code I write to use the function is gtData(car,toyota) Feb 8, 2013 at 2:04
0

Thanks to mnel, I realized using $ instead of "[" was the issue. He also provided good reference material.

But the solution was simple. Instead of calling the field inside the function with it's name it should be called by its number. so data[,x] works without a problem.

thanks mnel

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.