Give a dataframe in R
structure(list(Feat1 = c("A", "B", "A", "B"), Time = c("Start",
"Start", "Finish", "Finish"), Value = c("Value1", "Value2", "Value3",
"Value2"), feat2 = c("Alpha", "Bravo", "Alpha", "Bravo"), Frequency = c(2L,
4L, 5L, 3L)), class = "data.frame", row.names = c(NA, -4L))
I have both the frequencies and also features of a certain set of observations. I want all the frequencies to be converted to a number of rows equal to the frequency value while keeping the features.
As an example, the first row becomes these 2 rows because frequency was 2
Try uncount
:
library(tidyr)
dat %>%
uncount(Frequency)
output:
Feat1 Time Value feat2
1 A Start Value1 Alpha
2 A Start Value1 Alpha
3 B Start Value2 Bravo
4 B Start Value2 Bravo
5 B Start Value2 Bravo
6 B Start Value2 Bravo
7 A Finish Value3 Alpha
8 A Finish Value3 Alpha
9 A Finish Value3 Alpha
10 A Finish Value3 Alpha
11 A Finish Value3 Alpha
12 B Finish Value2 Bravo
13 B Finish Value2 Bravo
14 B Finish Value2 Bravo