【发布时间】:2020-07-17 08:13:38
【问题描述】:
我以 1000 Hz 的分辨率记录了血压和速度。在此记录期间,我确定了周期(例如 1、2、3)。主要问题是优化。我有多个长度在 10 到 15 分钟之间的录音,这导致 ~1.000.000 行。
n time pres mcav period
1 1 7.000 76 43.6 1
2 2 7.001 75 43.6 1
3 3 7.002 73 43.6 1
4 4 7.003 74 43.6 1
5 5 7.004 74 43.5 1
6 6 7.005 74 43.5 1
...
898914 909995 916.994 84 60.8 3
898915 909996 916.995 85 60.7 3
898916 909997 916.996 85 60.7 3
898917 909998 916.997 84 60.6 3
898918 909999 916.998 83 60.4 3
898919 910000 916.999 84 60.3 3
对于每个周期,我想识别块(3 秒周期)。
temp <- NULL
#For loop for every period
for(i in unique(df$period)){
#Extract the part of the df which is within the period
temp_df <- df[df$time >= min(df$time[df$period == i]) & df$time <= max(df$time[df$period == i]),]
#Insert "n" starting from 1 and count from there.
temp_df$block <- temp_df$n-min(temp_df$n)+1
#Divide this consecutive number into 3-second blocks.
temp_df$block <- ceiling(temp_df$block/3000)
#Combine the dataframes for every period into one.
temp <- rbind(temp,temp_df[,c("n","block")])
}
这个循环实际上很快,但可以优化。应用和自制功能是否可行?
下一部分可能是问题所在。 temp-dataframe 现在将被合并:
df <- merge(df,temp,by="n",all.x=T)
这部分需要几秒钟,但它会产生这个数据框:
n time pres mcav period block
1 1 7.000 76 43.6 1 1
2 2 7.001 75 43.6 1 1
3 3 7.002 73 43.6 1 1
4 4 7.003 74 43.6 1 1
5 5 7.004 74 43.5 1 1
6 6 7.005 74 43.5 1 1
...
898914 909995 916.994 84 60.8 3 100
898915 909996 916.995 85 60.7 3 100
898916 909997 916.996 85 60.7 3 100
898917 909998 916.997 84 60.6 3 100
898918 909999 916.998 83 60.4 3 100
898919 910000 916.999 84 60.3 3 100
【问题讨论】:
-
您可以使用
dplyr或特别是data.table进行很多优化。您可以dput()您的示例数据而不是发布打印输出以使其可重现吗? -
我真的更喜欢使用 base R,但如果优化仅适用于其他包,我接受。 1 mio 行,无法导出到
dput() -
但是您仍然可以通过选择几行然后
dput使其具有最低限度的可重复性,对吗?
标签: r for-loop optimization