0

I have a function like this:

func <- function(abc){
   idnumber <- data$abc
}

in which users can input the abc (a character string), and I want to get the column of that string. For example, if user inputs student, I want to get idnumber <- data$student but not data$student. Is there anyway I can do that?

989
  • 12,579
  • 5
  • 31
  • 53
Ziwei
  • 57
  • 1
  • 3
  • 11
  • There are two ways to do this. One you pass the variable as a string and utilize Psidom suggestions. Two you don't pass the variable as a string. To do this read this post. http://stackoverflow.com/questions/19133980/passing-a-variable-name-to-a-function-in-r/34079727#34079727. I believe that my answers is the superior approach. – Jacob H May 29 '16 at 23:29
  • data[[abc]] is also a possibility, as it also works for lists – Shape May 30 '16 at 01:54
  • Not clear. What do you actually want? A variable in the global environment by the name idnumber and "func" wou ld have what value? – IRTFM May 30 '16 at 03:44

1 Answers1

0

Use:

Data[ abc_val]

Or

Data[[ abc_val]]

Where abc_val is a character value perhaps equal to "abc". Really should pass a value to "Data", though.

IRTFM
  • 258,963
  • 21
  • 364
  • 487