📜  在 R 编程中将字符串转换为表达式 - parse()函数

📅  最后修改于: 2022-05-13 01:55:41.024000             🧑  作者: Mango

在 R 编程中将字符串转换为表达式 - parse()函数

R 语言中的parse()函数用于将字符类的对象转换为表达式类的对象。

示例 1:

# 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)

输出:

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

示例 2:

# 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)

输出:

[1] "2^3"
[1] 8