我正试图想出一种方法将一些数据加在一起。我想带一个候选人的载体加入(可以排除其中一些)与另一个候选人的载体(所有应该使用,或至少,应该指出未找到匹配)。我希望加入是1比1。在我卷起袖子并为此自己制定算法之前,我想我会问我所寻找的机器是否已经存在。如果没有工作R代码,也许一些完善的算法?理想情况下,我也希望解决方案能够使用日期。
以下是我认为重要的一些案例:
#' # case: all y in xs
#' c(1, 2, 3, 4, 5, 6) %>% closest_to(c(4, 5, 6)) # => c(4, 5, 6)
#' # case: all xs in y
#' c(3, 5) %>% closest_to(c(1, 2, 3, 4, 5)) # => c(NA, NA, 3, NA, 5)
#' c(2, 5) %>% closest_to(c(2, 4, 5, 6)) # => c(2, NA, 5, NA)
#' # case: no xs in y
#' c(1, 2, 3) %>% closest_to(c(4, 5, 6)) # => c(1, 2, 3)
#' c(1, 2) %>% closest_to(c(4, 5, 6)) # => c(NA, 1, 2)
#' c(5, 6) %>% closest_to(c(1, 2)) # => c(1, 2)
#' # case: xs is empty
#' c() %>% closest_to(c(1, 2)) # => c(NA, NA)
#' # case: xs is y
#' c(1, 2, 3) %>% closest_to(c(1, 2, 3)) # => c(1, 2, 3)
#' # case: some values shared (revert to minimum distance of maximum length)
#' c(1, 2, 5) %>% closest_to(c(2, 4, 5, 6)) # => c(1, 2, 5, NA)
#' c(2, 5) %>% closest_to(c(1, 3, 6)) # => c(NA, 2, 5) (choose some nice way to handle ties)
答案 0 :(得分:1)
在这里,我找到了一些可以与您的问题相匹配的答案,但最后一个答案。
#' # case: all y in xs
#' c(1, 2, 3, 4, 5, 6) %>% closest_to(c(4, 5, 6)) # => c(4, 5, 6)
vector1<-c(1, 2, 3, 4, 5, 6)
closest_to<-c(4,5,6)
intersect(vector1, closest_to)
#[1] 4 5 6
#' # case: all xs in y
#' c(3, 5) %>% closest_to(c(1, 2, 3, 4, 5)) # => c(NA, NA, 3, NA, 5)
#' c(2, 5) %>% closest_to(c(2, 4, 5, 6)) # => c(2, NA, 5, NA)
#' # case: no xs in y
#' c(1, 2, 3) %>% closest_to(c(4, 5, 6)) # => c(1, 2, 3)
#' c(1, 2) %>% closest_to(c(4, 5, 6)) # => c(NA, 1, 2)
#' c(5, 6) %>% closest_to(c(1, 2)) # => c(1, 2)
vector2<-c(3, 5)
closest_to2<-c(1, 2, 3, 4, 5, 6)
closest_to2 %in% vector2
#[1] FALSE FALSE TRUE FALSE TRUE FALSE
which(closest_to2 %in% vector2)
#[1] 3 5
#' # case: xs is empty
#' c() %>% closest_to(c(1, 2)) # => c(NA, NA)
c() %in% c(1, 2)
#logical(0)
#' # case: xs is y
#' c(1, 2, 3) %>% closest_to(c(1, 2, 3)) # => c(1, 2, 3)
vector1<-c(1,2,3)
vector2<-c(1,2,3)
setequal(vector1, vector2)
#[1] TRUE
#' # case: some values shared (revert to minimum distance of maximum length)
#' c(1, 2, 5) %>% closest_to(c(2, 4, 5, 6)) # => c(1, 2, 5, NA)
#' c(2, 5) %>% closest_to(c(1, 3, 6)) # => c(NA, 2, 5) (choose some nice way to handle ties)
# No idea
干杯!,