错误代码:mutate_impl(.data,dots)中的错误:评估错误:k< = n不为TRUE

时间:2017-10-22 17:05:30

标签: r zoo

我在尝试找出数据中的错误时遇到了问题,我希望有人会有一些见解。错误代码是:

Error in mutate_impl(.data, dots) : Evaluation error: k <= n is not TRUE

我可以让它处理小型数据集,甚至我的数据子集。每370-380行发生一次错误。我看到的模式是数据中的“第1周”,每年只有一次。

以下是.csv格式的数据链接:

http://www.filedropper.com/stackoverflowdata

这是我正在使用的代码:

    i = 3
QBrollavgDT <- data.table(rankDT)
QBrollavgDT <- QBrollavgDT[, c("Player_Name", "Season", "Week", "FDP")]
#QBrollavgDT <- subset(QBrollavgDT, Player_Name == "K.Collins")                 <- This works
#QBrollavgDT <- QBrollavgDT[c(381,759,1140,1510,1888,2264.2641), ]              <- these are the rows that thorw the error

        #begin calculating the rolling average and eliminating players with < i games played

        QBrollavgDT <- setDT(QBrollavgDT)[, if (.N >= i) .SD, by = Player_Name] #this eliminates all players with < i games played
        QBrollavgDT <- QBrollavgDT %>% #this is the code that puts the rolling average on and names the column RollAvg
                arrange(Player_Name, Season, Week) %>% 
                group_by(Player_Name, Season) %>% 
                mutate(RollAvg = zoo::rollapply(FDP, i, mean, by = 1, align = "right", fill = NA))

它必须与周和季节有关,但我不明白如何。我尝试了所有可以找到的解决方案,但无法正常工作。

1 个答案:

答案 0 :(得分:0)

发生错误是因为您没有足够的观察值来应用函数。

rollaply中,您将宽度指定为i3),并且您的某些Player_Name & Season组合没有i次观察。例如:

library(data.table)
i <- 3

# Generate data
QBrollavgDT <- fread("stackoverflowdata.csv")
d <- QBrollavgDT[, if (.N >= i) .SD, by = Player_Name][order(Week)]

# Produces OPs error
d[, zoo::rollapply(FDP, i, mean, by = 1, align = "right", fill = NA), .(Player_Name, Season)]

# Subset data where Player_Name & Season combinations have >= i observations
d2 <- d[, .N, .(Player_Name, Season)][N >= i]
d3 <- merge(d, d2)
# Apply rollaply on subseted data
d3[, zoo::rollapply(FDP, i, mean, by = 1, align = "right", fill = NA), .(Player_Name, Season)]

# Result
         Player_Name Season        V1
   1:       A.Dalton   2011        NA
   2:       A.Dalton   2011        NA
   3:       A.Dalton   2011 11.200000
   4:       A.Dalton   2011 15.826667
   5:       A.Dalton   2011 13.353333
  ---                                
2884: Z.Mettenberger   2014 11.220000
2885: Z.Mettenberger   2015        NA
2886: Z.Mettenberger   2015        NA
2887: Z.Mettenberger   2015  9.833333
2888: Z.Mettenberger   2015 10.126667

快速解决方案:
不仅可以SD计算Player_Name,还可以计算Season

d <- QBrollavgDT[, if (.N >= i) .SD, .(Season, Player_Name)][order(Week)]
d[, zoo::rollapply(FDP, i, mean, by = 1, align = "right", fill = NA), .(Player_Name, Season)]