R编程中计算绝对值——abs()方法
R编程中的abs()
方法用于获取绝对值,即正值不变,负值转换为正值。
Syntax: abs(value)
Return: Returns the absolute value.
示例 1:
# R program to calculate absolute value
# Using abs() method
answer1 <- abs(-12)
answer2 <- abs(21)
answer3 <- abs(-2)
print(answer1)
print(answer2)
print(answer3)
输出:
[1] 12
[2] 21
[3] 2
示例 2:
# R program to calculate absolute value
# Using abs() method
answer1 <- abs(c(1, 2, 3, 4))
answer2 <- abs(c(1, -2, 3, -4))
print(answer1)
print(answer2)
输出:
[1] 1 2 3 4
[2] 1 2 3 4