How to convert an expression into a string using deparse() in R
Overview
The deparse() function in R is used to convert an expression into a character string.
Syntax
deparse(expr)
Syntax for the deparse() function
Parameter value
The deparse() function takes the parameter value expr(), which represents the R expression that needs to be converted into a character string.
Return value
The deparse() function returns an object of the character class.
Code example
# creating an expressionexpr = expression(5 ^ 2)# evaluating the expressioneval(expr)# calling the deparse() functionmychar = deparse(expr)# evaluating the value of mycharmychar# checking the class of the resultclass(mychar)
Code explanation
- Line 2: We create an expression variable
expr, using theexpression()function. - Line 5: We evaluate the value of the expression in
expr(), using theeval()function.
- Line 8: We implement the
deparse()function on the expression we created inexpr. We assign the result to a variable calledmychar. - Line 11: We print the variable
mychar. - Line 14: We obtain the class of the variable
mychar.