I have a function below that contains an IF statement, the error message is:
1: In if (df == "iris1") { : the condition has length > 1 and only the first element will be used
Can anyone amend the code so that it works?
library(tidyverse)
iris1<-iris[1:50, ]
iris2<-iris[51:100,]
add_col<-function(df,colname)
{
df$newcol<-df [, colname]*100
if(df=="iris1"){df<-df%>%mutate(col_id="some text")}
if(df=="iris2"){df<-df%>%mutate(col_id="other text")}
return(df)
}
x <- c("iris1", "iris2")
z<-map(map(x, ~ as.symbol(.x) %>% eval),
~ add_col(.x, "Sepal.Length"))