R Function to extract values greater than x [duplicate] - r

This question already has answers here:
How can i select values from a vector in R using logical operators?
(3 answers)
Closed 1 year ago.
I am running a simulation model and would like to know how to extract all values greater than 6 in this gamma distribution. Thank you!
cost <- 100
n_samp <-1000
gamma<-rgamma(n_samp,2,0.5)

You can also subset the array gamma with a logical vector:
gt6_values = gamma[gamma > 6]

You can use subset from base R to get just the values greater than 6.
subset(gamma, gamma > 6)

Related

R Command for replacing value [duplicate]

This question already has answers here:
Replace given value in vector
(8 answers)
Closed 4 years ago.
hi just learning R and looking at how to do this.
How do i change a value of "1" in vehicle vector where it should be filled with the proper value from vendor$shop vector. I'm trying to make a weighted adjacency matrix
vehicle_vector[vehicle_vector == 1] <- new_value

creating a numeric vector in r [duplicate]

This question already has answers here:
Convert a matrix to a 1 dimensional array
(11 answers)
Closed 4 years ago.
I imported a 70104 x 1 matrix with hourly wind speeds into r. I am trying to fit various energy distributions to the data. however, when I try to fir the models, this error comes up "data must be a numeric vector of length greater than 1". How do I rectify this issue?
We can just wrap with c to convert it to a vector
v1 <- c(mat)
Or explicitly use as.vector to strip off the dim attributes
as.vector(mat)
data
set.seed(24)
mat <- matrix(rnorm(70104))

how to divided a vector and get the partition num in R? [duplicate]

This question already has answers here:
Dividing a vector into categories
(1 answer)
How to create a categorical variable in R with unequal categoris [closed]
(1 answer)
R if else with for loop [duplicate]
(3 answers)
Closed 5 years ago.
I'm programming in R.I need to divided a vector into x partitions(eg,x=4),and get the partition number of the vector, something like this...
a <- data.frame(x=1:20)
a$numofpartition<-ifelse(a$x<6,1,ifelse(a$x<11,2,ifelse(a$x<16,3,4)))
As the code,I divided a$x into 4 partitions,and get the partition number for each x, any functions in R could do this? Thank you!

How to calculate cumulative mean in R? [duplicate]

This question already has answers here:
Calculate cumulative average (mean)
(7 answers)
Closed 5 years ago.
(I am sorry if the term is not correct).
In R, I have a numeric vector x. I want to create new vector y where:
y[i] = mean (x[1:i)
It is easy to write a function to calculate y, but is there a built-in function in R which do the task?
Thank you very much
Try this
y <- cumsum(x) / seq_along(x)
Reference
https://stat.ethz.ch/pipermail/r-help/2008-May/162729.html

Counting number of sequences in a vector [duplicate]

This question already has answers here:
How can I count runs in a sequence?
(2 answers)
Closed 8 years ago.
I have a binary vector and I want to count how many sequences of 1's I've got. So that if I have a vector like:
bin <- c(1,1,0,1,1,1,1,0,0,0,1,0,1,1,0,0,1,1,1)
I would get 5. I haven't found any existing functions that could do this, anyone got any good tips on how one could write one? I don't know how to build the "counter" when the sequences all have different lengths.
The run length encoding function (rle) is built for this. Helpfully whilst it computes the length of runs of equal values in a vector, it returns those lengths with the values. So use rle( bin ).
Compare the $values output to your desired value (1) with == and sum the result (because you get a TRUE or 1L when the run of values is of 1's):
sum( rle(bin)$values == 1 )
[1] 5

Resources