-1

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
yoyomada
  • 14
  • 1
  • 3
    There is a famous R proverb: ["If the answer is parse() you should usually rethink the question."](https://cran.r-project.org/web/packages/fortunes/vignettes/fortunes.pdf) The answer to your question is `eval(parse())` which means that you should reconsider your whole approach. (You should do that anyway if you are storing/creating R code in Excel.) – Roland Jul 26 '18 at 08:26
  • Possible duplicate / Related post: https://stackoverflow.com/questions/1743698/evaluate-expression-given-as-a-string – zx8754 Jul 26 '18 at 08:30
  • That still does not work. It seems it's trying to read "text" as a file: "Error in mutate_impl(.data, dots) : Evaluation error: 'file' must be a character string or connection." – yoyomada Jul 26 '18 at 08:39
  • See above linked post, try `eval(parse(text="5+5"))`. – zx8754 Jul 26 '18 at 08:54

2 Answers2

0

After @Roland's comment, if you still want to do this, you can pass textas a variable in mutate_ and not mutate.

require(tidyverse)
#Sample dataset
df <- mtcars %>% 
  select(vs, gear, carb)

#Text to pass
text <- "ifelse(vs == 0, gear*carb, gear/carb)"

# Result
df %>% 
  mutate_(foo = text)
DJV
  • 4,743
  • 3
  • 19
  • 34
-2

I think this is creates the G_Age_of_Business column and is the simplest method:

# Setting up your initial data:    
varName <- "G_Age_of_Business"
tab=data.frame(Age_of_Business=22:26)
tab

# Here's the line that matters:
tab$G_Age_of_Business <-ifelse(tab$Age_of_Business < 24 & !is.na(tab$Age_of_Business), '<24', 'else')
tab  

# Output:
Age_of_Business G_Age_of_Business
# 1              22               <24
# 2              23               <24
# 3              24              else
# 4              25              else
# 5              26              else
stevec
  • 41,291
  • 27
  • 223
  • 311