在R中查找覆盖值
在上述数据中,Locx1是x的起点,Locx2是x的终点,Locy1是y的起点,Locy2是y的终点。我想找到y值,其中Locy1和Locy2的50%及以上在R中的Locx1和Locx2之间。我该怎么做 例如,第一行适合此示例。y的起点在Locx1和Locx2之间为119,并且148-119/168-119大于%50 谢谢将x和y的交点长度除以y的全长 数据: 如果我没弄错的话在R中查找覆盖值,r,data-manipulation,R,Data Manipulation,在上述数据中,Locx1是x的起点,Locx2是x的终点,Locy1是y的起点,Locy2是y的终点。我想找到y值,其中Locy1和Locy2的50%及以上在R中的Locx1和Locx2之间。我该怎么做 例如,第一行适合此示例。y的起点在Locx1和Locx2之间为119,并且148-119/168-119大于%50 谢谢将x和y的交点长度除以y的全长 数据: 如果我没弄错的话 d1 <- structure(list(ID = 1:6, Locx1 = c(100, 121, 146,
d1 <- structure(list(ID = 1:6, Locx1 = c(100, 121, 146, 194, 162, 182
), Locy1 = c(119, 173, 104, 164, 188, 142), Locx2 = c(148, 170,
184, 236, 196, 190), Locy2 = c(168, 180, 120, 210, 190, 213)), class = "data.frame", row.names = c(NA,
-6L))
这是一个有点冗长但希望也更透明的解决方案,其结果与@jay.sf相同
df %>%
filter((pmin(Locy2, Locx2) - pmax(Locy1, Locx1)) / (Locx2 - Locx1) >= 0.5)
ID Locx1 Locx2 Locy1 Locy2
1 1 100 148 119 168
2 6 182 190 142 213
由reprex软件包v0.3.0于2020-09-03创建,您可能会发现其他答案很有用。所以我误解了这个问题。你能用你的例子说明你想要的结果吗?
d1 <- structure(list(ID = 1:6, Locx1 = c(100, 121, 146, 194, 162, 182
), Locy1 = c(119, 173, 104, 164, 188, 142), Locx2 = c(148, 170,
184, 236, 196, 190), Locy2 = c(168, 180, 120, 210, 190, 213)), class = "data.frame", row.names = c(NA,
-6L))
df %>%
filter((pmin(Locy2, Locx2) - pmax(Locy1, Locx1)) / (Locx2 - Locx1) >= 0.5)
ID Locx1 Locx2 Locy1 Locy2
1 1 100 148 119 168
2 6 182 190 142 213
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
d1<-data.frame(ID=c(1:6),
Locx1=c(100,121,146,194,162,182),
Locx2=c(148,170,184,236,196,190),
Locy1=c(119,173,104,164,188,142),
Locy2=c(168,180,120,210,190,213))
d2 <- d1 %>% mutate(seqx = purrr::map2(Locx1, Locx2, .f = ~seq(.x, .y, 1)),
seqy = purrr::map2(Locy1, Locy2, .f = ~seq(.x, .y, 1)),
intersection = purrr::map2(seqx, seqy, .f = ~intersect(.x, .y)),
overlap = purrr::map2_dbl(intersection, seqy, .f = ~length(.x)/length(.y)),
my_condition = overlap >= 0.5
)
d2 %>% select(-contains('seq'), -intersection)
#> ID Locx1 Locx2 Locy1 Locy2 overlap my_condition
#> 1 1 100 148 119 168 0.6000000 TRUE
#> 2 2 121 170 173 180 0.0000000 FALSE
#> 3 3 146 184 104 120 0.0000000 FALSE
#> 4 4 194 236 164 210 0.3617021 FALSE
#> 5 5 162 196 188 190 1.0000000 TRUE
#> 6 6 182 190 142 213 0.1250000 FALSE