Получение знака элементов числового вектора в программировании на R - функция sign ()
Опубликовано: 17 Февраля, 2022
sign() function in R Language is used to find the sign of the elements of the numeric vector provided as argument. It does not work on complex vectors.
Syntax: sign(x)
Parameter:
x represents numeric vectorReturns: 1 for Positive number and -1 for Negative Number
Пример 1:
# Create a variablex <- 1y <- -100 # Check signcat("Sign of x:", sign(x), "
")cat("Sign of y:", sign(y)) |
Выход:
Знак x: 1 Знак y: -1
Example 2:
# Creating a vectorx <- c(1, 5, 0, -10, 100, -20, 10, -69) # Check Signcat("Sign of elements of vector x: ", sign(x), "
") |
Выход:
Знак элементов вектора x: 1 1 0 -1 1 -1 1 -1