Related Articles

Related Articles

Convert a String to an Expression in R Programming – parse() Function
  • Last Updated : 24 Jun, 2020

parse() function in R Language is used to convert an object of character class to an object of expression class.

Syntax: parse(text = character)

Parameters:
character: Object of character class

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to convert 
# character to expression
  
# Creating an object of character class
x <- "sin(pi / 2)"
  
# Class of object
class(x)
  
# Calling parse() Function
x1 <- parse(text = x)
  
# Class of parsed object
class(x1)

chevron_right


Output:

[1] "character"
[1] "expression"

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to convert 
# character to expression
  
# Creating an object of character class
x <- "2 ^ 3"
  
# Evaluating the value of object
eval(x)
  
# Calling parse() Function
x1 <- parse(text = x)
  
# Evaluating the value of object
eval(x1)

chevron_right


Output:

[1] "2^3"
[1] 8
My Personal Notes arrow_drop_up
Recommended Articles
Page :