1

可重现的数据如下图所示:

 library(pracma);library(zoo)
library(dplyr);library(tidyverse)
Tag<- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,5, 5,
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6)
Temp<- c(43, 44, 45, 41, 43, 38, 40, 41, 39, 37, 37, 39, 45, 42, 41, 43, 44, 39,38,
         37, 43, 44, 45, 41, 43, 38, 40, 41, 39, 37, 37, 39, 45, 42, 41, 43, 44, 
         39, 38, 37, 43, 44, 45, 41, 43, 38, 40, 41, 39, 37, 37, 39, 45, 42, 41,43,
         44, 39, 38, 37)
dfr=data.frame(Tag=Tag,Temp=Temp)
# Function for Applying Moving Average for x column
lag_apply <- function(x, n, callback){
  k = length(x);
  result = rep(0, k);
  for(i in 1 : (k - n + 1)){
    result[i] <- callback(x[i :  (i + n -1)]);
  }    
  return(result);
}
#Moving Average Calculation
dfr$MAvg_Temp<-ave(dfr$Tag,  
                      FUN= function(x) lag_apply(dfr$Temp, 5, function(x){mean(x)}) )
#Grouping by Tag and performing peak function over MAvg_Temp
result <- dfr %>% 
  split(.$Tag) %>% 
  map(~findpeaks(.$MAvg_Temp, nups = 1, ndowns = 0, zero = "+", peakpat = NULL, minpeakheight = 33, minpeakdistance = 4, threshold = 0.42, npeaks = 11, sortstr = FALSE)) %>% 
  map_df(~data_frame(Temp = parse_number(.x[,1]), 
                     Peak_Mid = parse_number(.x[,2]),
                     Peak_start = parse_number(.x[,3]),
                     Peak_End= parse_number(.x[,4])),
         .id = 'Tag') %>% 
  arrange(Tag, Peak_Mid) %>% 
  group_by(Tag) %>% 
  mutate(Trend= (lead(Temp)-Temp)/(lead(Peak_Mid)-Peak_Mid))  

数据说明:
第 1 列:标签(分组数据)。
第 2 列:温度数据数据。
第 3 列: 的移动平均线Column 2

在这里,当我计算findpeakfor Column 2(Temp) 时没有错误。但是,当我在数据框中的任何列中传递Moving Average`` in thefindpeak 函数Missing value where TRUE/FALSE NA NA` 时,我对错误一无所知。请指导我! I am getting error of **Missing Value TRUE/FALSE as shown below**.
[Error Message][1] From my understanding error says
.But when i check the column fori did'nt find any

运行上述代码后,我得到以下错误。
if (npeaks > 0 && npeaks < nrow(X)) { 中的错误:需要 TRUE/FALSE 的缺失值

4

0 回答 0