How Many Elements in a Vector Are Greater Than X Without Using a Loop

How many elements in a vector are greater than x without using a loop

Use length or sum:

> length(x[x > 10])
[1] 2
> sum(x > 10)
[1] 2

In the first approach, you would be creating a vector that subsets the values that matches your condition, and then retrieving the length of the vector.

In the second approach, you are simply creating a logical vector that states whether each value matches the condition (TRUE) or doesn't (FALSE). Since TRUE and FALSE equate to "1" and "0", you can simply use sum to get your answer.

Because the first approach requires indexing and subsetting before counting, I am almost certain that the second approach would be faster than the first.

How many elements of a vector are smaller or equal to each element of this vector?

You can also use the *apply family as follows,

sapply(x, function(i) sum(x <= i))
#[1] 1 3 7 6 5 4 3 8 9

Find position of first value greater than X in a vector

# Randomly generate a suitable vector
set.seed(0)
v <- sample(50:150, size = 50, replace = TRUE)

min(which(v > 100))

Finding value of a series in R without for-loop

You can use cumprod to get a cumulative product of a vector which is what you are after

p <- cumprod(z)
p
# [1] 0.6666667 0.5333333 0.4571429 0.4063492 0.3694084 0.3409923 0.3182595
# [8] 0.2995384 0.2837732 0.2702602 0.2585097 0.2481694 0.2389779 0.2307373
# [15] 0.2232941 0.2165276 0.2103411 0.2046562 0.1994087

A less-efficient but more generalized alternative to cumprod would be

p <- sapply(i, function(x) prod(z[1:x]))

Here the sapply takes the place of the loop and passes a different ending index for each product

Then you can do

1 + sum(p)

Counting number of elements greater than a certain value in a numy.ndarray

You can simply do:

import numpy

arr = numpy.asarray([0.25656927, 0.31030828, 0.23430803, 0.25999823, 0.20450112, 0.19383106, 0.35779405, 0.36355627, 0.16837767, 0.1933686, 0.20630316, 0.17804974, 0.06902786, 0.26209944, 0.21310201, 0.12016498, 0.14213449, 0.16639964, 0.33461425, 0.15897344, 0.20293266, 0.14630634, 0.2509769, 0.17211646, 0.3922994, 0.14036047, 0.12571093, 0.25565785, 0.18216616, 0.0728473, 0.25328827, 0.1476636, 0.1873344, 0.12253726, 0.16082433, 0.20678088, 0.33296013, 0.03104548, 0.14949016, 0.05495472, 0.1494042, 0.32033417, 0.05361898, 0.14325878, 0.16196126, 0.15796155, 0.10990247, 0.14499696])

print((arr > 0.19214945092486838).sum())

The output is: 21

Check if all values in list are greater than a certain number

Use the all() function with a generator expression:

>>> my_list1 = [30, 34, 56]
>>> my_list2 = [29, 500, 43]
>>> all(i >= 30 for i in my_list1)
True
>>> all(i >= 30 for i in my_list2)
False

Note that this tests for greater than or equal to 30, otherwise my_list1 would not pass the test either.

If you wanted to do this in a function, you'd use:

def all_30_or_up(ls):
for i in ls:
if i < 30:
return False
return True

e.g. as soon as you find a value that proves that there is a value below 30, you return False, and return True if you found no evidence to the contrary.

Similarly, you can use the any() function to test if at least 1 value matches the condition.

number of values in a list greater than a certain number

You could do something like this:

>>> j = [4, 5, 6, 7, 1, 3, 7, 5]
>>> sum(i > 5 for i in j)
3

It might initially seem strange to add True to True this way, but I don't think it's unpythonic; after all, bool is a subclass of int in all versions since 2.3:

>>> issubclass(bool, int)
True

while-loop and for-loop in R

V <- 1:10
X <- 14
Sum <- 0
n <- 0

# For each i between 1 and 10
for (i in 1:length(V)){
# Sum is equal to the sum of the i first terms
Sum <- sum(V[1:i])
# I increment n by 1
n <- n + 1
# if Sum is greater than X I get out of the for loop
if (Sum > X) {
break
}
}
print(Sum)
print(n)

V <- 1:10
X <- 14
Sum <- 0
n <- 1

# as long as Sum is smaller or equal than X
while (Sum <= X){
# Sum is the sum of the first n terms of V
Sum <- sum(V[1:n])
# I increment n by 1
n <- n + 1
}

print(Sum)
print(n-1)


Related Topics



Leave a reply



Submit