I store formulas for computation of variables in a excel file. I read the formulas as a char variable and am trying to apply them in a mutate statement.
> text
[1] "ifelse(Age_of_Business<24 & !is.na(Age_of_Business), '1. <24', '2. else')"
> varName
[1] "G_Age_of_Business"
> tab=data.frame(Age_of_Business=22:26)
> tab
Age_of_Business
1 22
2 23
3 24
4 25
5 26
> tab=mutate(tab,!!varName:=text)
> tab
Age_of_Business
1 22
2 23
3 24
4 25
5 26
G_Age_of_Business
1 ifelse(Age_of_Business<24 & !is.na(Age_of_Business), '1. <24', '2. else')
2 ifelse(Age_of_Business<24 & !is.na(Age_of_Business), '1. <24', '2. else')
3 ifelse(Age_of_Business<24 & !is.na(Age_of_Business), '1. <24', '2. else')
4 ifelse(Age_of_Business<24 & !is.na(Age_of_Business), '1. <24', '2. else')
5 ifelse(Age_of_Business<24 & !is.na(Age_of_Business), '1. <24', '2. else')
However, the formula is not evaluated and it is just written as text.
Any ideas? I am not keen on using dplyr, I just do not want to hardcode the dataframe name in the input excel file.
Later edit: it seems that the problem is actually the dot in the text variable. R thinks it is a file:
> tab=tab %>% mutate_(x=eval(parse(text)))
Error in parse(text) : 'file' must be a character string or connection