R语言 获取数字向量元素的符号 – sign() 函数
R语言中的 sign() 函数用于查找作为参数的数字向量元素的符号。它对复数向量不起作用。
语法: sign(x)
参数:
x 代表数字向量
返回: 正数为1,负数为1。
例子 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