YouTip LogoYouTip

R Func Abs

# Absolute value of a vector x <-c(-12, 27, -3, 4.2, -2, 0, -54, 21, -4, 2) result.abs<-abs(x) print(result.abs)

Output of the above code:

 5 3.14  12.0 27.0 3.0 4.2 2.0 0.0 54.0 21.0 4.0 2.0

abs() is very commonly used in calculating errors:

Example

# Predicted and actual values

 actual <-c(100, 150, 200, 250, 300)

 predicted <-c(98, 155, 195, 260, 310)

# Calculate absolute error

 error <- actual - predicted

 abs_error <-abs(error)

print(abs_error)

# Calculate Mean Absolute Error (MAE)

 mae <-mean(abs_error)

print(paste("Mean Absolute Error MAE:", mae))

Output of the above code:

 2 5 5 10 10 "Mean Absolute Error MAE: 6.4"

Image 4: R Language Examples R Language Examples

← R Func AppendSlash Commands β†’