Home > Blockchain >  How to assign function argument to variable name
How to assign function argument to variable name

Time:09-22

Say I have these data:

data <- data.frame(a = c(1,2,3))

And a simple function that creates a data frame. It also creates a new variable based on a simple function; this new variable takes the name that is passed in with varname. Here is my attempt (the assign line is wrong):

fun <- function(varname) {
    data <- data.frame(a = c(1,2,3))
    assign(paste0("data$", varname), sqrt(data$a))  
    data
}

fun("newvar")

Base R or tidyverse solutions are both great.

CodePudding user response:

You were close! There are multiple ways of subsetting dataframes, including using the [[.]] notation (e.g., data[["var"]]. Simply assigning a value to a new column initializes the column.

fun <- function(varname) {
    data <- data.frame(a = c(1,2,3))
    data[[varname]] <- sqrt(data$a)
    data
}

fun("newvar")

CodePudding user response:

tidyverse

If you want to pass the variable name as a string then a tidyverse method would be:

library(dplyr)

fun <- function(varname) {
  data.frame(a = c(1,2,3)) %>% 
    mutate(!! varname := sqrt(a))
}

fun("newvar")

Alternatively, you could use tidyeval so you don't have to quote the variable name:

library(dplyr)

fun <- function(varname) {
  varname <- rlang::enquo(varname)
  data.frame(a = c(1,2,3)) %>% 
    mutate(!! varname := sqrt(a))
}

fun(newvar)

base R

If you want to use base R I would recommend the solution posted by @Noah, but another base R option that is fairly obtuse would be:

fun <- function(varname) {
  data.frame(a = c(1,2,3)) |>
    within(eval(substitute(x <- sqrt(a), list(x = as.name(varname)))))
}

fun("newvar")
  • Related