當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 parse()用法及代碼示例


parse()R語言中的函數用於將字符類對象轉換為表達式類對象。

用法: 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

相關用法


注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Convert a String to an Expression in R Programming – parse() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。