Compute Derivative of an Expression in R Programming - deriv() and D() Function Last Updated : 30 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report In R programming, derivative of a function can be computed using deriv() and D() function. It is used to compute derivatives of simple expressions. Syntax: deriv(expr, name) D(expr, name) Parameters: expr: represents an expression or a formula with no LHS name: represents character vector to which derivatives will be computed Example 1: r # Expression or formula f = expression(x^2 + 5*x + 1) # Derivative cat("Using deriv() function:\n") print(deriv(f, "x")) cat("\nUsing D() function:\n") print(D(f, 'x')) Output: Using deriv() function: expression({ .value <- x^2 + 5 * x + 1 .grad <- array(0, c(length(.value), 1L), list(NULL, c("x"))) .grad[, "x"] <- 2 * x + 5 attr(.value, "gradient") <- .grad .value }) Using D() function: 2 * x + 5 Example 2: r # Little harder derivative # Using deriv() Function cat("Using deriv() function:\n") print(deriv(quote(sinpi(x^2)), "x")) # Using D() Function cat("\nUsing D() function:\n") print(D(quote(sinpi(x^2)), "x")) Output: Using deriv() function: expression({ .expr1 <- x^2 .value <- sinpi(.expr1) .grad <- array(0, c(length(.value), 1L), list(NULL, c("x"))) .grad[, "x"] <- cospi(.expr1) * (pi * (2 * x)) attr(.value, "gradient") <- .grad .value }) Using D() function: cospi(x^2) * (pi * (2 * x)) Comment More infoAdvertise with us Next Article Compute Derivative of an Expression in R Programming - deriv() and D() Function U utkarsh_kumar Follow Improve Article Tags : R Language R Math-Function Similar Reads Evaluate an Expression in R Programming - eval() Function eval() function in R Language is used to evaluate an expression passed to it as argument. Syntax: eval(expr) Parameters: expr: expression to be evaluated Example 1: Python3 1== # R program to evaluate an expression # Calling eval() Function eval(sin(pi / 3)) eval(5 + 2) eval(cos(2)) Output: [1] 0.86 1 min read Create an Expression in R Programming - expression() Function expression() function in R Language is used to create an expression from the values passed as argument. It creates an object of the expression class. Syntax: expression(character) Parameters: character: Expression, like calls, symbols, constants Example 1: Python3 1== # R program to create an expres 1 min read Evaluating an Expression in R Programming - with() and within() Function with() function in R programming evaluates the expression in an environment constructed locally by the data and does not create a copy of the data. Syntax: with(data, expr) Parameters: data represents dataset to be used expr represents formula or expression to be evaluated Example 1: Python3 # Creat 2 min read Compute Density of the Distribution Function in R Programming - dunif() Function dunif() function in R Language is used to provide the density of the distribution function. Syntax: dunif(x, min = 0, max = 1, log = FALSE) Parameters: x: represents vector min, max: represents lower and upper limits of the distribution log: represents logical value for probabilities Example 1: r # 1 min read Modify Data of a Data Frame with an Expression in R Programming - with() Function with() function in R Language is used to modify data of a data frame by evaluating an expression within the arguments of the function. Syntax: with(x, expr) Parameters: x: Data frame expr: Expression to modify data Example 1: Python3 1== # R program to modify data of an object # Calling predefined d 1 min read Like