《R语言_找波峰波谷求波长_果蝇心管数据处理.docx》由会员分享,可在线阅读,更多相关《R语言_找波峰波谷求波长_果蝇心管数据处理.docx(5页珍藏版)》请在优知文库上搜索。
1、R语言一找波峰波谷求波长一果蝇心管数据处理ZW2024-03-04加载xlsx包&导入Excel数据# 载入R包Iibrary(Xlsx)library(ggplot2)library(ggalt)ggalt,:# #RegisteredS3methodsoverwrittenby# #methodfrom# #grid.draw.absoluteGrobggplot2# #grobHeight.absoluteGrobggplot2# #grobWidth.absoluteGrobggplot2# #grobX.absoluteGrobggplot2# #grobV.absoluteGrob
2、ggplot2# 导入心管界面内径的测量数据sheets-getSheets(IoadWorkbook(E:Desktopheart_tube.lsx)# ExceL表格名称names(sheets)# #1,SheetlSheet2Sheet3Sheet4,Sheet5,# 每个表格生成变量for(iinl:length(sheets)assign(paste(Dm_heart_dti,ijsep=),read.xlsx(E:WDesktopWheart_tube.lsx,sheetindex=header=F)# 所有表格合并为一个ListDm_heart_dtsheet2=Dm_hear
3、t_dt_2$X2,sheet3=Dm_heart_dt_3$X2sheet4=Dm_heart_dt_4$X2sheet5=Dm_heart_dt_5$X2)# 作折线图plot(Dm_heart_dt_l$X2type=,b)# 美化折线图ggplot(Dm-heart-dt-ljaes(Xl,X2)+geom_xspline(spline_shape=0.5)+labs(=Time,jy=Length)+theme_test(base_size=20)+theme_classic(base_size=18,base_family=,serif)# #Warning:Usingthesiz
4、eaestheticinthisgeomwasdeprecatedinggplot23.4.0.# #iPleaseuselinewidthinthedefault_aesfieldandelsewhereinstead.# #Thiswarningisdisplayedonceevery8hours.# #Calllifecycle:last_lifecycle_warnings()toseewherethiswarningwas# #generated.# 保存图片ggsave(Dm_heart_dt_l.png9width=8,height=5)波峰波谷变量x,波峰=(x-l)=(x+I
5、),波谷=(x-l)S(x+l)# 波峰peak-function(x)x_maxs-c()x_max-c()x_i-c()for(iin2:(length(x)-l)if(xi-l=i+l)x_max-xix_maxs-c(x_maxs,x_max)x_i-C(X_i,i)y-data.frame(peak_x=x_i,peak_y=x_maxs)return(y)peak_test-peak(Dm_heart_dt_l$X2)# 施客valley-function(x)x_mins-c()x_min-c().ii&xixi+l)x_min-xix_mins-c(x_mins,x_min)x
6、_i-c(x_i,i)y-data.fname(valley_x=x_i,valley_y=x_mins)return(y)# 找波峰波谷简化:# 波峰peakl-function(x)x_maxs-c()x_max-c()for(iin2:(length(x)-1)if(xi-l=xi+l)x_max-ix_maxs-C(X_maxs,x_max)return(x_maxs)peakl_data-Iapply(Dm_heart_dt,peakl)# 波容valleyl-function(x)x_mins-c()x_min-c().ixi&xixi+l)x_min-xix_mins-c(x_m
7、ins,x_min)x_i-c(x_i,i)y-data.fname(valley_x=x_i,valley_y=x_mins)return(y)# 找波峰波谷求波峰持续时间# 分类汇总:求波峰个数,适合波峰值不一致的数据peak_count_a-aggregate(=peak_test$peak_y,by=list(peak_test$peak_y),FUN=length)# rLe函数计算向量中连续重复元素的长度:求波峰个数peak_count_r-rle(peak_test$peak_y)$lengthpeak_counts-lapply(peakl_data,rle)# 茄诙持续时间计
8、算peak_length-list()for(iin1:length(peak_counts)peak_lengthi-peak_countsi$lengths*0.4)# 改list内的名称names(peak_length)-names(sheets)capture.output(peak_length,file=peak_length.txt)求邻近波峰间距# 求邻近波峰间距wavelength-function(x)wl-c()for(iin2:(length(x)-2)if(xi-lxi+l)xiapo-ifor(minxiapo:(length(x)-2)if(xm-l=xm+l)
9、shangpo-mbreakwl-c(wljshangpo-xiapo)return(wl)# 示例wavelength(Dm_heart_dt_3$X2)# #1565656566# 批量wave_length-list()for(iinl:length(sheets)Dm_h_d-get(paste(Dm_heart_dt_n,i,sep=,)wave_lengthi-wavelength(Dm_h_d$X2)*0.4# 改List内的名称names(wave_length)-names(sheets)capture.output(wave_length,file=wave_length.txt)