简体   繁体   中英

dplyr: `group_by` and `mutate_each` error

I'm having trouble figuring out why my dplyr group_by / mutate_each function is bombing out. When I run mutate_each with the default n=2, it works. Both it doesn't work with n=1 or n=3.

library(dplyr)

## moving average function
get.mavg <- function(mycol, n=2) {
  require(zoo)
  mycol <- na.locf(mycol, na.rm=FALSE)
  if(length(mycol) < n) 
    return(mycol)
  c(mycol[1:(n-1)], rollapply(mycol, width=n, mean, align="right"))
}

dummy <- data_frame(Name1=c("A","A","A","A","B","B","B"),
                    Name2=c("B","B","C","C","C","C","C"),
                    stat = c(0,5,5,10, 10,5,5),
                    day = c(1:4, 1:3) )
dummy %>%
  group_by(Name2) %>%
  mutate_each(funs(get.mavg(.,3)), stat:day)

Error: incompatible types, expecting a integer vector

If I try passing the columns individually to my moving average function, it works!

get.mavg(dummy[dummy$Name2=="B",]$stat, 3)
# [1] 0 5
get.mavg(dummy[dummy$Name2=="C",]$stat, 3)
# [1] 5.000000 10.000000  8.333333  8.333333  6.666667

What's going on?

There's a bug where if you have a mix of double and numeric, you get an error. I think this is your problem here.

If you alter your function, to ensure you always return a double:

get.mavg <- function(mycol, n=2) {
  require(zoo)
  mycol <- na.locf(mycol, na.rm=FALSE)
  if(length(mycol) < n) 
    return(as.double(mycol))
  c(mycol[1:(n-1)], rollapply(mycol, width=n, mean, align="right"))
}

It works fine for me:

dummy %>%
  group_by(Name2) %>%
  mutate_each(funs(get.mavg(.,3)), stat:day)
Source: local data frame [7 x 4]
Groups: Name2 [2]

  Name1 Name2      stat      day
  (chr) (chr)     (dbl)    (dbl)
1     A     B  0.000000 1.000000
2     A     B  5.000000 2.000000
3     A     C  5.000000 3.000000
4     A     C 10.000000 4.000000
5     B     C  8.333333 2.666667
6     B     C  8.333333 2.333333
7     B     C  6.666667 2.000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM