R语言 把字符串转换为表达式 – parse()函数
R语言中的 parse() 函数用于将一个字符类的对象转换为表达式类的对象。
语法: parse(text = character)
参数:
character: 字符类的对象
例子 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
极客教程