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 diff
erence 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")
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
Regex Expression to Match Decimal Numbers With Comma as a Separator
Remove Space Between Plotted Data and the Axes
Ggplot2: Setting Geom_Bar Baseline to 1 Instead of Zero
How to Add a Row to Data Frame Based on a Condition
Choose the Top Five Values from Each Group in R
How to Sum a Variable by Group
Quickly Reading Very Large Tables as Dataframes
Aggregating by Unique Identifier and Concatenating Related Values into a String
Convert a List to a Data Frame
How to Specifically Order Ggplot2 X Axis Instead of Alphabetical Order
Apply a Function to Every Specified Column in a Data.Table and Update by Reference
Include Levels of Zero Count in Result of Table()
How to Do Vlookup and Fill Down (Like in Excel) in R
Plot a Legend Outside of the Plotting Area in Base Graphics
How to Plot Two Histograms Together in R