0

I have a function

my_function <- function(input_1, input_2){
a = cbind(df$input_1, df$input_2)
}

I want to allow an input parameter such that it can be put in as a $ sign column reference, as shown in the function. I know that this directly won't work. Perhaps, some sort of paste function will work? I'm not sure.

Does anyone know how to do that?

Thanks in advance!!

user4918087
  • 421
  • 1
  • 6
  • 14

1 Answers1

1

You need to use the double brackets for this:

my_function <- function(input_1, input_2){
  a = cbind(df[[input_1]], df[[input_2]])
}

v <- c(1,2,3)
w <- c(4,5,6)

df <- data.frame(v,w)

print(my_function("v","w"))

      [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
Mike Wise
  • 22,131
  • 8
  • 81
  • 104