我从数据框中提取了以下向量
parent<-c(119.04832, 120.34873, 405.80245, 635.80934)
product<-c(119.04838, 800.56437, 361.87349, 582.09567, 444.65898, 286.08075)
我想删除所有元素&#34;几乎相等&#34;仅来自产品阵列。嵌套的for循环可以是一个选项。像这样:
limit<-0.0001
for(i in 1:length(parent)){
for(j in 1:length(product)){
if(abs(parent[i]-product[j]<=limit)){
replicate<-rbind(replicate,product[j])
} else {not_replicate<-rbind(not_replicate, product[j])
}
}
}
所以,我期望得到一个新的载体
>replicate
[1] 800.56437, 361.87349, 582.09567, 444.65898, 286.08075
有没有比这更聪明,更快的方法?例如暗示使用sapply或ifelse的东西?
P.S。 一个更大的测试案例可能是:
parent<-c(225.08619,236.09266.16304,267.14706,270.10044,277.18305,282.15796,355.15658,496.23987,238.11061,239.06545,252.08988,252.08988,
,369.17223,413.25640,467.30356,551.39344,748.50853)
product<-c(300.02900,227.10570,341.17340,305.17360,241.12110,369.18980,269.13730,347.10386,373.19980,357.18960,266.16307,233.19210,269.13720,357.18860,269.13740,406.24220,305.17370,405.22580,157.08490,157.08520,157.09490,169.08480,169.12130,181.12060,
187.09550,199.11050,209.11600,225.08620,227.12690)
答案 0 :(得分:2)
您可以使用sapply
代替for循环
product[!as.logical(apply(sapply(product, function(x) abs(x - parent) <= 0.0001), 2L, sum))]
答案 1 :(得分:2)
以下是使用dplyr软件包near
函数的示例:
parent<-c(119.04832, 120.34873, 405.80245, 635.80934)
product<-c(119.04838, 800.56437, 361.87349, 582.09567, 444.65898, 286.08075)
library(dplyr)
#create matrix of matches between parent and product
result<-sapply(parent, function(x){near(x, product, tol=0.0001)})
#find rows where there is at least one match
toremove<-which(rowSums(result)>0)
#remove unwanted values
product[-toremove]
答案 2 :(得分:2)
假设:
parent <- c(119.04832, 120.34873, 405.80245, 635.80934)
product <- c(119.04838, 800.56437, 361.87349, 582.09567, 444.65898, 286.08075)
limit <- 0.0001
我会使用outer
:
product[!as.logical(colSums(abs(outer(parent, product, "-")) <= limit))]
# [1] 800.5644 361.8735 582.0957 444.6590 286.0808