I have this example dataframe "df":
id <- c(1001, 1002)
col2 <- c(5, 2)
col3 <- c(1, 4)
df <- data.frame(id, col2, col3)
Is there an easy way to convert this data frame so that the new data frame contains same column names but assigns "1" to the nth row that corresponds to each value and assigns "0" to the remaining slots? It seemed doable but somewhat difficult. The resulting table will be as follows (df_results):
id <- c(rep(1001, 5), rep(1002, 5))
col2 <- c(0,0,0,0,1, 0,1,0,0,0)
col3 <- c(1,0,0,0,0,0,0,0,1,0)
df_results <- data.frame(id, col2, col3)
You can uncount() using the parallel max of your cols, then grouping by id, check if the value equals the row number:
library(dplyr)
library(tidyr)
df %>%
uncount(pmax(col2, col3)) %>%
group_by(id) %>%
mutate(across(starts_with("col"), ~ as.numeric(.x == row_number()))) %>%
ungroup()
# A tibble: 9 × 3
id col2 col3
<dbl> <dbl> <dbl>
1 1001 0 1
2 1001 0 0
3 1001 0 0
4 1001 0 0
5 1001 1 0
6 1002 0 0
7 1002 1 0
8 1002 0 0
9 1002 0 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With