我在Stack上提问时很新,所以如果我弄错了,请原谅我。这是一个场景(我试图用一个简单的例子重现它):
library("pracma")
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)
数据描述 - 我们有两列:
标记 [组明变量]
温度(必须执行峰值功能的数值变量)
for (i in 1:6) {
df=filter(dfr , dfr$Tag == i)
pik =findpeaks(df$Temp, nups = 1, ndowns = 0, zero = "+", peakpat = NULL,
minpeakheight = 33, minpeakdistance = 4,
threshold =0.42, npeaks = 11, sortstr = FALSE)#Peak Function
pik<- as.data.frame(pik)#Converting into data frame as it is in matrix form
names(pik) <- c("Temp","Peak_Mid","Peak_start","Peak_End")# renaming the header
pik <- arrange(pik , Peak_Mid)#Rearranging with Peak_Mid
attach(pik)#attaching pik df
j=1#initializing for loop
s=0#initializing for loop
for (j in 1:nrow(pik))#for loop for calculating slope individual points
s[[j]]=((Temp[j+1]-Temp[j])/(Peak_Mid[j+1]-Peak_Mid[j]))
pik$Trend <- 0#creating new column(Trend) filled with zero
pik$Trend <- s# inserting the calculated s variable onto pik df
w[[i]]=as.data.frame(pik)
}
我试图将上述代码转换为for
循环,以便每i
个值i:e(Tag[i]
在我们的案例i
范围内根据我们的数据,1至6)。因此,Tag[i]
每次通过峰值函数计算,然后我们将计算点之间的斜率,我们将获得一个包含4列的新数据框。
将对每个Tag[i]
执行此计算,i
是主数据帧的子集。因此,我们将得到{{1}}个不同的数据帧,这些数据帧将与rbind一起标记为no。
这是具有预期输出的输入视觉:
答案 0 :(得分:2)
使用我们可以执行的tidyverse
库:
result <- dfr %>%
split(.$Tag) %>%
map(~findpeaks(.$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))
这将按顺序:
dataset
值将原始list
拆分为datasets
Tag
。 (split
)dataset
,执行findpeaks
函数,使用提供的参数,结果为matrix
。 (map
)matix
每个data.frame
广告,请重命名。 (data_frame
)data.frame
。 (map_df
)arrange
)Trent
列。 (mutate
)希望这有帮助