Luc
Luc

Reputation: 958

R Run text in quotes

I am trying to create a function that uses the data used in a survfit-object.

Example:

library(survival)
fit <- survfit(Surv(time, status) ~ factor(sex), data = subset(lung, ph.ecog  <=1)) 

fit$call[3] # This gives me the data part of the survfit function
> subset(lung, ph.ecog <= 1)()

I'd like to create a dataset using fit$call[3]:

tempdata <- subset(lung, ph.ecog <= 1)

where the 'subset(lung, ph.ecog <= 1)' part is extracted from the survfit object.

 tempdata <- do.call(as.character(fit$call)[3])  # Doesn't work

Upvotes: 2

Views: 705

Answers (1)

cyberj0g
cyberj0g

Reputation: 3797

You should use eval and remove parenthesis:

eval(parse(text = gsub('()','',fit$call[3]))) 

Upvotes: 3

Related Questions