Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

r - Add a new column, based on data in between zeroes

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))

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")

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An option with base 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"

Output:

   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

About 0.0038 seconds for 3,000 rows ;) Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...