This question already has answers here:
Find nearest smaller number
(7 answers)
Closed 2 years ago.
Building on R - Fastest way to find nearest value in vector, I am interested in getting the nearest value in a vector prior to a specific value.
The DescTools package Closest does not differentiate according to direction.
Eg.
x=c(1,7:10)
min(DescTools::Closest(x, 6, which = F, na.rm = FALSE))
would return 7, while I want 1. Anyone?
You could try writing a simple function to do this.
closest_preceding <- function(vec, value) max(vec[vec < value])
closest_preceding(x, 6)
#> [1] 1
Related
This question already has answers here:
How to return 5 topmost values from vector in R?
(4 answers)
Closed last month.
I'm trying to obtain the three largest number in a vector, with R.
With function max() I can get the first one, is there any function that I can choose the number of values that I want?
For example:
vector <- c(100,215,200,180,300,500,130)
max(vector)
This will returne 500,
I want something that returns: 500, 300, 215.
I've tried
pmax(vector,3)
you can use the tail function to get the last three elements of the sorted vector.
Example:
largest_three <- tail(sort(vector), 3)
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)
This question already has answers here:
How to retrieve the most repeated value in a column present in a data frame
(9 answers)
Closed 2 years ago.
I was given a sample vector v and was asked to use R code to extract, as a number (meaning: not as a character string), the value that was repeated the most times in v.
(Hints: use table(); note that which.max() gives you index of a vector's maximum value, like the maximum value within a table; names() allows you the extract the values of the original vector, when applied to the output of table().)
My answer is as follows:
names(which.max(table(v)))
it returns the correct answer as a string, not as a number. Am i using the hint correctly? Thanks.
names return the number as character, perhaps add as.integer/as.numeric to convert it to number.
as.integer(names(which.max(table(v))))
Moreover, in case of tie which.max would return only the first maximum. If you want all the values which are tied you can use :
v <- c(1, 1, 2, 4, 5, 3, 3)
as.integer(names(which.max(table(v))))
#[1] 1
tab <- table(v)
as.integer(names(tab[max(tab) == tab]))
#[1] 1 3
This question already has answers here:
How to index a vector sequence within a vector sequence
(5 answers)
Closed 5 years ago.
I have got a dataframe and I need to find row numbers where the values of the entries in one column match a certain pattern.
Let the col1 col1 = matrix(c(1,0,0,0,0,0,0,0,0,0,2,0,2,0,0,0,0,0,0,0,1), nrow = 21, ncol = 1) be an example of by column and vector r r = c(2, 0 ,2) be a vector I need to match it with.
I need R to return an index number of rows where the pattern in r matches the values in col1 (in this case row 11, 12, 13).
I thought I could achieve this with row.match, but that is not the case. I have tried different combinations of match function, but it doesn't yield any results either.
Maybe the way I am approaching this problem is wrong from the beginning, but I have trouble believing that there isn't any function, that would provide me with the expected result given some adjustment.
Thanks.
You could do this using rollapply from zoo. Basically, this runs identical on a rolling basis with a window of length(r). This tells you that the sequence is present starting at positon 11 of the col1 vector..
library(zoo)
which(rollapply(col1,length(r),identical,r))
[1] 11
To get a vector of positions, you could do:
which(rollapply(col1,length(r),identical,r))+0:(length(r)-1)
[1] 11 12 13
This question already has answers here:
return index from a vector of the value closest to a given element
(3 answers)
Closed 5 years ago.
I would like to identify the closest date in a vector of given date. Let's say I have the following date vector (with 5 random dates):
coldate= as.Date(c("2013-08-03", "2013-09-04", "2013-09-08", "2013-09-12", "2013-11-01"));
Now, I want to find the closest date to x = as.Date("2013-10-01") inside this vector.
Here is my code :
> which((coldate-x) == min(coldate-x))
[1] 1
The result should be 4, since the date "2013-09-12" is the closest. But, I have 1... What's wrong in my code?
you miss an abs to take care of negative values:
which(abs(coldate-x) == min(abs(coldate - x)))
[1] 4
See also the which.min function:
R> which.min(abs(x-coldate))
[1] 4
The which.closest() function from the birk package is a simple option.
coldate= as.Date(c("2013-08-03", "2013-09-04", "2013-09-08", "2013-09-12", "2013-11-01"))
x = as.Date("2013-10-01")
which.closest(coldate, x)
[1] 4