在 R 编程中获取数值向量元素的符号 – sign()函数
R 语言中的sign()
函数用于查找作为参数提供的数值向量的元素的符号。它不适用于复杂的向量。
Syntax: sign(x)
Parameter:
x represents numeric vector
Returns: 1 for Positive number and -1 for Negative Number
示例 1:
# Create a variable
x <- 1
y <- -100
# Check sign
cat("Sign of x:", sign(x), "\n")
cat("Sign of y:", sign(y))
输出:
Sign of x: 1
Sign of y: -1
示例 2:
# Creating a vector
x <- c(1, 5, 0, -10, 100, -20, 10, -69)
# Check Sign
cat("Sign of elements of vector x: ", sign(x), "\n")
输出:
Sign of elements of vector x: 1 1 0 -1 1 -1 1 -1