I need to convert a character created by paste function to formula to be executed automatically.
Consider the following data frame:
z=data.frame(x=c(1,2,3))
Now storing the variable names in z
names=names(z)
and z contains one name as
names[1]
[1] "x"
Now assign the first column values to this name as
assign(names[1],z[,1])
Using paste function to build the following result
A=paste(c(names[1],"z"), collapse = "=")
B=paste0(A,"[,1]")
B
[1] "x=z[,1]"
it is clear that structure of B is a character
str(B)
chr "x=z[,1]"
****What I need is to convert this character to formula or run (not manually)** **
x=z[,1]
**But when I use **
as.formula(B)
I get
Error in formula.default(eval(parse(text = x, keep.source = FALSE)[[1L]])) : invalid formula
Note: I don't want to use eval parse or assign get as they are not formatted as a formula