Search code examples
rrlang

Reference R data frame column name as a string, given only the column name


I have a data frame df. It has a column named b. I know this column name, although I do not know its position in the data frame. I know that colnames(df) will give me a vector of character strings that are the names of all the columns, but I do not know how to get a string for this particular column. In other words, I want to obtain the string "b". How can I do that? I imagine this may involve the rlang package, which I have difficulty understanding.

Here's an example:

library(rlang)
library(tidyverse)

a <- c(1:8)
b <- c(23,34,45,43,32,45,68,78)
c <- c(0.34,0.56,0.97,0.33,-0.23,-0.36,-0.11,0.17)
df <- data.frame(a,b,c)

tf <- function(df,MYcol) {
  print(paste0("The name of the input column is ",MYcol)) # does not work
  print(paste0("The name of the input column is ",{{MYcol}})) # does not work
  y <- {{MYcol}} # This gives the values in column b as it shoulkd
}
z <- tf(df,b) # Gives undesired values - I want the string "b"

Solution

  • If you cannot pass column name as string in the function (tf(df,"b")) directly, you can use deparse + substitute.

    tf <- function(df,MYcol) {
      col <- deparse(substitute(MYcol))
      print(paste0("The name of the input column is ",col)) 
      return(col)
    }
    
    z <- tf(df,b) 
    #[1] "The name of the input column is b"
    z
    #[1] "b"