Closest date in a vector to a given date [duplicate] - r

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

Related

Subtract values from vector [duplicate]

This question already has answers here:
What does the diff() function in R do? [closed]
(2 answers)
Closed 5 months ago.
I dont know what´s the posible way of creating a new vector from a previous one by subtraction
of the second element to the first one and doing it for all the values of the vector, by
applying Xi-X(i-1)
Use diff
> x <- c(5,2,3,4)
> diff(x)
[1] -3 1 1

Get nearest value in vector prior to specific value [duplicate]

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

Finding the most repeated value using table() function [duplicate]

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

Is there a way to determine which 2 dates a specific date is between? [duplicate]

This question already has an answer here:
Find index of value in a sorted vector in R
(1 answer)
Closed 3 years ago.
I am trying to find out which two date values of a vector a specific date is between. I am not really sure how else to explain it, the example should help more.
## Vector of dates
temp <- seq(as.Date("2000/01/01"),as.Date("2003/01/01"),"years")
temp
[1] "2000/01/01" "2001/01/01" "2002/01/01" "2003/01/01"
date<- sample(seq(as.Date("2000/01/01"),as.Date("2003/01/01"),"days"),1)
date
This should be a random date, but just for the example let say that it is 2002/09/14. How can I go about having date look through temp and find the values that it is between, so for this example, the answer would be c("2002/01/01","2003/01/01").
I am basically looking for something that is the flip of the between function in dplyr.
You can use findInterval
set.seed(123)
date<-sample(seq(as.Date("2000/01/01"),as.Date("2003/01/01"),"days"),1)
date
#[1] "2001-02-18"
ind <- findInterval(date, temp)
c(temp[ind], temp[ind + 1])
#[1] "2001-01-01" "2002-01-01"

Cutting value in vector by determine positions [duplicate]

This question already has answers here:
Trying to return a specified number of characters from a gene sequence in R
(3 answers)
Extracting the last n characters from a string in R
(15 answers)
Closed 5 years ago.
Is there a function in R that I can cut a value in vector.
for example i got this vec:
40754831597
64278107602
64212163451
and each vale in the vec i want to cut so from the number pos 3 to 6 for example and get a new vector look like this
7548
2781
2121
and so on
I don't really get why you would like to do this, but here you go:
# assuming it's a character vector
substring(vec,3,6)
# if it's numeric
substring(as.character(vec),3,6)
#output
#[1] "7548" "2781" "2121"
We can use sub
sub(".{2}(.{4}).*", "\\1", v1)
#[1] "7548" "2781" "2121"
data
v1 <- c(40754831597, 64278107602, 64212163451)

Resources