aorali
aorali

Reputation: 93

Indexes overlap error when using dplyr to run a function

For the follwing example

n=68
    dat <- data.frame(group=rep(LETTERS[1:2], n/2),
                      x=rnorm(n, 0.1,0.1))

I am trying to run a function (FitDoubleLogBeck from greenbrown) by group:

SNDVI0= dat %>%
  group_by(group) %>%
  dplyr::summarise(smoothNDVI= FitDoubleLogBeck(x, tout = F, weighting = T, 
                                                hessian = F, plot = F, ninit = 10)$predicted)

But I got this erro

Error in `dplyr::summarise()`:
! Problem while computing `smoothNDVI = ...$predicted`.
i The error occurred in group 2: group = "B".
Caused by error in `rbind.zoo()`:
! indexes overlap

When I run the function for each group separately it work fine

dat0= subset(dat,group== "A" )
fit <- FitDoubleLogBeck(dat0$x, out = F, weighting = T, 
                         hessian = F, plot = F, ninit = 10)$predicted)
fit$predicted

How I can run the function by grup using dplyr? The original data has more than 6000 group and each group has 52 observations.

Upvotes: 2

Views: 111

Answers (1)

dcsuka
dcsuka

Reputation: 2997

You can try just making the zoo output into a list as such:

dat %>%
  group_by(group) %>%
  dplyr::summarise(smoothNDVI= list(FitDoubleLogBeck(x, tout = F, weighting = T, 
                                                hessian = F, plot = F, ninit = 10)$predicted))

# # A tibble: 2 × 2
#   group smoothNDVI
#   <chr> <list>    
# 1 A     <zoo [1]> 
# 2 B     <zoo [1]> 

Or alternatively, making it into a tibble:

dat %>%
  group_by(group) %>%
  dplyr::summarise(smoothNDVI= as_tibble(FitDoubleLogBeck(x, tout = F, weighting = T, 
                                                hessian = F, plot = F, ninit = 10)$predicted))

# # A tibble: 2 × 2
#   group smoothNDVI$value
#   <chr>            <dbl>
# 1 A                0.124
# 2 B                0.125

I noticed the output is different every time, and I am not sure how intentional that is.

Upvotes: 1

Related Questions