I am having difficulty understanding how to get R to treat a character as the name of a variable from a data frame.
I'm attempting to do something like the following
library(dplyr)
library(survival)
df <- data_frame(time = rexp(30),
cens = sample(0:1, 30, replace = TRUE),
var = rnorm(30))
var_int <- 'var'
survfit(Surv(time, cens) ~ var_int, data = df)
I am aware that something like this would work
survfit(Surv(time, cens) ~ df[[var_int]], data = df)
but more generally I'm trying to understand how I can get R to treat 'var'
as the variable var
coming from df
rather than the character itself.
I had thought this may work, but it does not, and I don't quite understand how to apply eval()
appropriately in this case.
survfit(Surv(time, cens) ~ eval(var_int), data = df)