Lookup values corresponding to the closest date

后端 未结 2 1174
时光说笑
时光说笑 2020-12-22 09:05

I have a data.frame x with date and Value

x = structure(list(date = structure(c(1376534700, 1411930800, 1461707400, 
1         


        
相关标签:
2条回答
  • 2020-12-22 09:41
    sapply(y, function(z) x$Value[which.min(abs(x$date - z))])
    # [1] 40 65 44 67 44
    
    0 讨论(0)
  • 2020-12-22 09:49

    Using data.table you can join to the nearest value

    library(data.table)
    
    x <- as.data.table(x)
    y <- data.table(date=y)
    
    res <- x[y, on='date', roll='nearest']
    
    0 讨论(0)
提交回复
热议问题