r语言 - 错误代码:mutate_impl(.data, dots) 中的错误:评估错误:k <= n 不是 TRUE



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

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))

它一定与星期和季节有关,但我不明白如何。我尝试了我能找到的所有解决方案,但无法正常工作。

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

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)]

相关内容

最新更新