简体   繁体   中英

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"

We can use as_string with enquo/ensym

tf <- function(df, MYcol) {
 
 mycol <- rlang::as_string(rlang::ensym(MYcol))
  print(glue::glue("The name of the input column is {mycol}")) 
  return(mycol)
}

z <- tf(df,b) 
The name of the input column is b
z
#[1] "b"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM