Counting the Number of Elements With the Values of X in a Vector

Counting the number of elements with the values of x in a vector

You can just use table():

> a <- table(numbers)
> a
numbers
4 5 23 34 43 54 56 65 67 324 435 453 456 567 657
2 1 2 2 1 1 2 1 2 1 3 1 1 1 1

Then you can subset it:

> a[names(a)==435]
435
3

Or convert it into a data.frame if you're more comfortable working with that:

> as.data.frame(table(numbers))
numbers Freq
1 4 2
2 5 1
3 23 2
4 34 2
...

Counting the number of elements in vector x which equal any vector element in vector y

Try length-which-in:

length(which(x %in% y))

R - Counting elements in a vector

Since you are counting continual sequence here, we can use rle.

rle(vec)$lengths
#[1] 2 2 2 2

Something similar with data.table rleid :

table(data.table::rleid(vec))
#1 2 3 4
#2 2 2 2

Count of number of elements between distinct elements in vector

Or

ave(seq.int(x), x, FUN = function(x) c(NA, diff(x)))
# [1] NA NA 2 NA 2 4 1 4 1 3 1 7 1 1 6 1 1 1 8 6

We calculate the first difference of the indices for each group of x.


A data.table option thanks to @Henrik

library(data.table)
dt = data.table(x)
dt[ , d := .I - shift(.I), x]
dt

Counting the number of elements with the values of x in a vector

You can just use table():

> a <- table(numbers)
> a
numbers
4 5 23 34 43 54 56 65 67 324 435 453 456 567 657
2 1 2 2 1 1 2 1 2 1 3 1 1 1 1

Then you can subset it:

> a[names(a)==435]
435
3

Or convert it into a data.frame if you're more comfortable working with that:

> as.data.frame(table(numbers))
numbers Freq
1 4 2
2 5 1
3 23 2
4 34 2
...

In R, How to count the total number of items in a vector greater than the absolute value of 1

The abs should be on the 'vec' and not on 1

sum(abs(vec) > 1)
[1] 7

how would you count the number of elements that are true in vector?

You've made a few different errors here. The solution should look something like this.

Start by defining your variables and drawing your samples from the exponential distribution using rexp(100, 0.001):

r <- c(0.1, 0.2, 0.25, 0.5, 1, 2, 4, 5, 10)

set.seed(69) # Make random sample reproducible
x <- rexp(100, 0.001) # 100 random samples from exponential distribution
y <- rexp(100, 0.001) # 100 random samples from exponential distribution

Rsample <- x/y

The tricky part is getting the proportion of Rsample that is less than each value of r. For this we can use sapply instead of a loop.

props <- sapply(r, function(x) length(which(Rsample < x))/length(Rsample))

We get the cdf from the pdf by integrating (not shown):

cdf_at_r <- 1/(-r-1)  # Integral of 1/(1+r)^2 at above values of r

And we can see what happens when we plot the proportions that are less than the sample against the cdf:

plot(cdf_at_r, props)

# What do we notice?
lines(c(-1, 0), c(0, 1), lty = 2, col = "red")

Sample Image

Created on 2020-03-05 by the reprex package (v0.3.0)

How can I count the number of elements of a given value in a matrix?

Have a look at Determine and count unique values of an array.

Or, to count the number of occurrences of 5, simply do

sum(your_matrix == 5)


Related Topics



Leave a reply



Submit