user3547456
user3547456

Reputation: 59

Pass tags to data frame within function

I'm new at R and have a pretty simple question. I'd like to make a function like:

myfunc <- function(tag, value){
  data.frame(tag = value)
} 

And pass it:

myfunc(example, 10)

But what I get is:

Error in data.frame(tag = value) : object 'example' not found

And what Id like to get is:

  example
1      10

In other words, its not interpreting my input as the tag I want. Is there a good way around this?

Thank you

Upvotes: 1

Views: 42

Answers (1)

Tyler Rinker
Tyler Rinker

Reputation: 109864

Not sure what you're trying to do so telling us that may result in better answers but you need to quote "exampe" as follows:

myfunc <- function(tag, value){
  setNames(data.frame(value), tag)
} 

myfunc("example", 10)

Or use as.character(substitute()) which is usually not a good idea:

myfunc <- function(tag, value){
  setNames(data.frame(value), as.character(substitute(tag)))
} 

myfunc(example, 10)

Upvotes: 2

Related Questions