I have a two column dataframe that has the value in the left column and frequency of that value in the right column. I want to reflect this data in a new dataframe that is just one column.
I have got it working with the 2 for loops below, but with my data (100k+ rows and many dataframes) its very slow. I've tried using the apply functions but cant work it out.
library(tidyverse)
twocol <- tribble(
~value, ~count,
0.23076923, 5,
0.69076923, 3,
1.15230769, 4,
1.61384615, 4,
2.15230769, 3
) %>% as.data.frame()
make_onecol <- function(df) {
dfnew <- data.frame(value=NA)
df %>% filter(count!=0) -> df
for (i in 1:nrow(df)) {
n <- df[i, 2]
for (j in 1:n) {
dfnew <- rbind(dfnew, df[i, 1])
}
}
return(dfnew)
}
onecol <- make_onecol(twocol)