Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

Splitting Data Frame based on a specific group ID(In R)

$
0
0

I have a data frame where I want to split by a group based on a single group and would like to split it so I can encapsulate everything from Valley 1 to Valley 2, Valley 2 to Valley 3 and so on.

Time      Value Peaks  Peak_id

1   0.00  455   Data    1
2   0.36  456   Data    2
3   0.75  456   Valley  1
4   1.14  459   Peak    1
5   1.54  456   Data    3
6   1.93  456   Data    4   
7   2.32  455   Valley  2   
8   2.72  458   Peak    2   
9   3.12  455   Valley  3   
10  3.51  458   Peak    3   
11  3.90  455   Valley  4   
12  4.30  456   Data    5   
13  4.69  461   Peak    4   
14  5.08  458   Data    6   
15  5.47  458   Valley  5   
16  5.87  459   Data    7   

Desired  output:
df1

3   0.75  456   Valley  1
4   1.14  459   Peak    1
5   1.54  456   Data    3
6   1.93  456   Data    4   
7   2.32  455   Valley  2   

df2
7   2.32  455   Valley  2   
8   2.72  458   Peak    2   
9   3.12  455   Valley  3       

df3

9   3.12  455   Valley  3   
10  3.51  458   Peak    3   
11  3.90  455   Valley  4       

df4

11  3.90  455   Valley  4   
12  4.30  456   Data    5   
13  4.69  461   Peak    4   
14  5.08  458   Data    6   
15  5.47  458   Valley  5   

Data in format for direct use in R:

dat <- structure(list(Row = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16), Time = c(0, 0.36, 0.75, 1.14, 1.54, 1.93, 2.32, 
2.72, 3.12, 3.51, 3.9, 4.3, 4.69, 5.08, 5.47, 5.87), Value = c(455, 
456, 456, 459, 456, 456, 455, 458, 455, 458, 455, 456, 461, 458, 
458, 459), Peaks = c("Data", "Data", "Valley", "Peak", "Data", 
"Data", "Valley", "Peak", "Valley", "Peak", "Valley", "Data", 
"Peak", "Data", "Valley", "Data"), Peak_id = c(1, 2, 1, 1, 3, 
4, 2, 2, 3, 3, 4, 5, 4, 6, 5, 7)), row.names = c(NA, -16L), class = c("tbl_df", 
"tbl", "data.frame"))

Viewing all articles
Browse latest Browse all 201977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>