programing tip

변수 (객체) 이름을 문자열로 변환하는 방법

itbloger 2020. 8. 30. 07:48
반응형

변수 (객체) 이름을 문자열로 변환하는 방법


변수 이름이있는 다음 데이터 프레임이 있습니다 "foo".

 > foo <-c(3,4);

내가하고 싶은 "foo"것은 문자열 로 변환 하는 것입니다. 따라서 함수에서 다른 추가 변수를 다시 만들 필요가 없습니다.

   output <- myfunc(foo)
   myfunc <- function(v1) {
     # do something with v1
     # so that it prints "FOO" when 
     # this function is called 
     #
     # instead of the values (3,4)
     return ()
   }

당신은 사용할 수 있습니다 deparsesubstitute함수 인수의 이름을 가져올 수 :

myfunc <- function(v1) {
  deparse(substitute(v1))
}

myfunc(foo)
[1] "foo"

참고 URL : https://stackoverflow.com/questions/14577412/how-to-convert-variable-object-name-into-string

반응형