have been trying to programme a custom function that outputs the variable name as a string from an input object x that is a specific vector from a dataframe, i.e. in the form of df$vector , so that it function like this
function(iris$Species)
>"Species"
Currently I am doing this:
vector.name<-function(x){
require(stringr)
#convert df$variable into string
xname <- as.character(deparse(substitute(x)))
if (str_detect(xname,"$")==T) {
str_split(xname,"$")
}
}
but the results are unsatisfying
> vector.name(iris$Species)
[[1]]
[1] "iris$Species" ""
I have tried both strsplit(){base} and str_split(){stringr}, they both work normally for other ordinary alphabetic strings, e.g.
> str_split(as.character(deparse(substitute(iris$Species))),"S")
[[1]]
[1] "iris$" "pecies"
How do I extract "vector" from df$vector in a custom function then?