【发布时间】:2020-05-06 17:31:24
【问题描述】:
dfdf<-data.frame(a= c(80,90,100,110,120),
b= c(500,400,300,200,100))
index= 102
如何在a列中找到最接近102的值,并在b列中返回同一行的值?
预期输出: 300
#attempt 1
index2<-min(abs(dfdf$a- index))
dfdf$b[dfdf$a- index==index2] # error sometimes positive values and other times the value is negative
##output:
>numeric(0)
【问题讨论】:
-
你想用确定的最接近的数字填充整个列吗?如果是,当你有两个最接近的数字时会发生什么?
-
不,我只想返回“b”列的值,它与“a”列的值在同一行,更接近 102。