I have a dataframe as follows:
df <- data.frame(ID = 1:3, A = c(2, 0, 1), B = c(0, 3, 2))
# ID A B
# 1 1 2 0
# 2 2 0 3
# 3 3 1 2
I want to replicate each column for N times where N means the maximum in that column. The column names are filled into the replicated columns for corresponding times and aligned left.
The expected output:
# ID A1 A2 B1 B2 B3
# 1 1 A A NA NA NA
# 2 2 NA NA B B B
# 3 3 A NA B B NA
I prefer base or tidyverse solutions. data.table is welcome but I'm unfamiliar to it though.
Thanks for any helps!
A tidyr solution with uncount() duplicating rows according to each number in the data:
library(tidyr)
df %>%
pivot_longer(-ID) %>%
uncount(value) %>%
pivot_wider(id_cols = ID, values_from = name, values_fn = list, names_sort = TRUE) %>%
unnest_wider(-ID, names_sep = '')
# # A tibble: 3 × 6
# ID A1 A2 B1 B2 B3
# <int> <chr> <chr> <chr> <chr> <chr>
# 1 1 A A NA NA NA
# 2 2 NA NA B B B
# 3 3 A NA B B NA
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