【问题标题】:Using approx in dplyr在 dplyr 中使用 approx
【发布时间】:2016-08-23 22:40:26
【问题描述】:

我正在尝试使用点xyear 之间的数据框中的每个id 进行线性近似。 dplyr 似乎是一个合适的选择,但由于错误,我无法让它工作:

错误:大小不兼容 (9),预期为 3(组大小)或 1

示例代码:

library(dplyr)
dat <- data.frame(id = c(1,1,1,2,2,2,3,3,3), year = c(1,2,3,1,2,3,1,2,3), x = c(1,NA,2, 3, NA, 4, 5, NA, 6))

# Linear Interpolation
dat %>% 
  group_by(id) %>% 
  mutate(x2 = as.numeric(unlist(approx(x = dat$year, y = dat$x, xout = dat$x)[2])))

样本数据:

  id year  x
1  1    1  1
2  1    2 NA
3  1    3  2
4  2    1  3
5  2    2 NA
6  2    3  4
7  3    1  5
8  3    2 NA
9  3    3  6

【问题讨论】:

标签: r dplyr approximation


【解决方案1】:

这里有几个方法(从 cmets 转移过来的):

1) na.approx/ave

library(zoo)

transform(dat, x2 = ave(x, id, FUN = na.approx))

年份为 1、2、3 我们不需要指定它,但如果需要,那么:

nr <- nrow(dat)
transform(dat, x2 = ave(1:nr, id, FUN = function(i) with(dat[i, ], na.approx(x, year))))

2) na.approx/dplyr

library(dplyr)
library(zoo)

dat %>% 
    group_by(id) %>% 
        mutate(x2 = na.approx(x, year)) %>% 
    ungroup()

如果不需要年份,则省略 na.approx 的第二个参数。

注意:zoo还有其他的NA填充功能,尤其是na.splinena.locf

【讨论】:

    【解决方案2】:

    您可以在基础 R 中执行此操作:

    dat <- dat[order(dat$id, dat$year),]
    dat$x2 <- unlist(by(dat, dat$id, function(df) approx(df$year, df$x, xout = df$year)[2]))
    dat
      id year  x  x2
    1  1    1  1 1.0
    2  1    2 NA 1.5
    3  1    3  2 2.0
    4  2    1  3 3.0
    5  2    2 NA 3.5
    6  2    3  4 4.0
    7  3    1  5 5.0
    8  3    2 NA 5.5
    9  3    3  6 6.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多