Search code examples
rdplyrtidyrexpand

Expand dataframe shows `x` is not a regular sequence error


I am having trouble expanding the following dataframe.

library(tidyr)
library(dplyr)
options(digits = 10)
library(readxl)

The following is the dataframe

df1= data.frame("ID"=c("A", "A", "A", "A", 'A', "B", 'B', "B", "B", 'B'), 
"A_Frequency"=c(1,2,3,4,5, 2.788,3.122,4,4.888,6), 
"A_Axis"=c(1,2,3,4,5,6,7,8,9,12),
"B_Frequency"=c(2,3,4,6,7, 2.677, 2.977, 3.877, 4.788, 7), 
"B_Axis"=c(1,2,3,4,5,7,8,9,10,14))

We now create df2 as follows

df2<- df1 %>%group_by(ID) %>%rowwise() %>% mutate(Sfreq = 
 min(na.omit(c(A_Frequency, B_Frequency))))

Next I am expanding the dataframe as follows

df3<- df2%>%group_by(ID)%>%expand(Sfreq=full_seq(Sfreq,0.001))

I am getting the following error

Error: x is not a regular sequence.

I have tried changing the expand function as follows

 df3<- df2%>%group_by(ID)%>%expand(Sfreq=full_seq(Sfreq,0.000001))

However, this either doesn't work or on occasion slows the code. Is there an alternate way to achieve the same.


Solution

  • Based on your description, you need complete, not expand. Note that you first need to ungroup your data frame since it is grouped rowwise.

    library(tidyverse)
    
    ungroup(df2) %>% 
     group_by(ID) %>% 
     complete(Sfreq = seq(min(Sfreq), max(Sfreq), by = 0.001))