展开数据框显示“x”不是常规序列错误

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

我在扩展以下数据框时遇到问题。

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

以下是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))

我们现在创建df2如下

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

接下来我将数据框扩展如下

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

我收到以下错误

Error: x is not a regular sequence.

我试过如下更改扩展函数

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

但是,这要么不起作用,要么有时会减慢代码速度。有没有其他方法可以达到同样的效果。

根据您的描述,您需要 complete,而不是 expand。请注意,您首先需要 ungroup 您的数据框,因为它是按行分组的。

library(tidyverse)

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