Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replicate each column for certain times

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!

like image 962
user18894435 Avatar asked Feb 04 '26 04:02

user18894435


1 Answers

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
like image 65
Darren Tsai Avatar answered Feb 05 '26 22:02

Darren Tsai