'Please help me in understanding this piece of code

I have just started learning R from Datacamp and got stuck at the following function:

below_zero <- function(x) {
    return(x[x < 0])
}

Website says that this function does the following:

We already created a function, below_zero(), that takes a vector of numerical values and returns a vector that only contains the values that are strictly below zero.

This function will be applied to this list called "temp":

[[1]] [1] 3 7 9 6 -1

[[2]] [1] 6 9 12 13 5

[[3]] [1] 4 8 3 -1 -3

[[4]] [1] 1 4 7 2 -2

[[5]] [1] 5 7 9 4 2

[[6]] [1] -3 5 8 9 4

[[7]] [1] 3 6 9 4 1

But, I'm not really able to understand this part in particular:

(x[x < 0])

If for e.g., x[1] returns the 1st element in the vector then what exactly is [x < 0] doing?

Is x < 0 returning a logical statement or really a number?

Please explain as to what this piece of code is doing.

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source