0

I have a R data frame.Let's say my data,df_sample looks like below

var1  var2 var3 var4
12    22   32   42
23    11   22   32
25    15   12   11    

I want to write a function which will take data frame name & variable name,not the index as per requirement, as input & will print a title.

so I tried this

    test <- function(data, var) {
      Title=paste("Check for:",var)
      message(Title)
    }
test(df_sample,var1)

But I'm getting error message as

Error in paste("Check for:",var) object 'var1' not found

can you please help me to resolve this issue?

Sonia
  • 464
  • 10
  • 21
  • 1
    `test(df_sample,"var1")`, or perhaps you want to try [non-standard evaluation](http://adv-r.had.co.nz/Computing-on-the-language.html)? – r2evans Oct 17 '18 at 19:32
  • 1
    If you want to pass the name of the variable in without quotes, you'll need `substitute()` to turn it into an expression and then get it's string representation. It's much easier just to pass in a string though. – MrFlick Oct 17 '18 at 19:34
  • It's difficult for me to pass variable name with string like this `var1` as the data I shared is sample data actual data contains 100 columns. So I will use a for loop to get the column name & then store it in another variable & then I will pass that variable in test. I will try substitute() – Sonia Oct 18 '18 at 09:55

0 Answers0