根据零之间的数据添加新列 [英] Add a new column, based on data in between zeroes
问题描述
我每秒收集一次功率数据(功率)(样本)。因此,我的data.frame的结构如下:
I have power data (Power) collected every second (Sample). My data.frame is therefore structured as follows:
Test <- data.frame(Sample = c(1:20),
Power = c(0,0,0,0,0,50,67,100,92,0,0,0,36,89,36,0,0,0,89,90))
功率输入的数量取决于人类骑自行车努力并偶尔休息的力量。因此,电源不会以有序的方式出现。由于没有标记来指示何时开始和停止工作,因此我想包括这个细节。当功率> 0且可以基于数据组一起评估每个工作的开始/停止时,可以描述工作。
The number of power entries is dependent upon a human performing an effort on a bike and resting sporadically. Therefore, power does not appear in an ordered fashion. As there are no markers to indicate when an effort starts and stops, I want to include this detail. An effort can be characterised when power > 0 and the start/ stop of each effort can be assessed based on data group together.
我现在希望包括一个新列(标记),用于查找分组在一起并用零分隔的功率数据。例如,我的预期输出将是:
I now wish to include a new column (Marker) that looks for power data grouped together and separated by zeroes. For example, my anticipated output would be:
Test$Marker <- c("Rest","Rest","Rest","Rest","Rest","Effort 1","Effort 1","Effort 1","Effort 1",
"Rest","Rest","Rest","Effort 2","Effort 2","Effort 2","Rest","Rest","Rest",
"Effort 3","Effort 3")
不幸的是,我的原始数据的长度大于3000行,因此手动执行此操作将很繁琐!
Unfortunately my raw data is > 3000 rows long, so to do this manually would be tedious! How do I please go about doing this in R?
推荐答案
以R为基数的选项:
indx1 = with(rle(Test$Power>0),rep(values,lengths))
indx2 = with(rle(Test$Power>0),rep(cumsum(values),lengths))
Test$Effort[indx1] = paste0("Effort",indx2[indx1])
Test$Effort[!indx1]="Rest"
输出:
Sample Power Effort
1 1 0 Rest
2 2 0 Rest
3 3 0 Rest
4 4 0 Rest
5 5 0 Rest
6 6 50 Effort1
7 7 67 Effort1
8 8 100 Effort1
9 9 92 Effort1
10 10 0 Rest
11 11 0 Rest
12 12 0 Rest
13 13 36 Effort2
14 14 89 Effort2
15 15 36 Effort2
16 16 0 Rest
17 17 0 Rest
18 18 0 Rest
19 19 89 Effort3
20 20 90 Effort3
3000行约0.0038秒;)希望这会有所帮助!
About 0.0038 seconds for 3,000 rows ;) Hope this helps!
这篇关于根据零之间的数据添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!