Вы можете создать фиктивный столбец и использовать pivot_wider
из tidyr
:
library(dplyr)
df %>%
mutate(n = 1) %>%
select(-sku_id) %>%
tidyr::pivot_wider(names_from = sku_name, values_from = n,
names_prefix = 'sku_', values_fill = list(n = 0))
# id sku_Google sku_AMZ sku_FK sku_AB sku_JIOMART sku_CLIQ sku_AMART
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 1 1 0 1 1 0 0 0
#2 2 0 1 0 0 1 0 0
#3 3 0 0 0 0 0 1 0
#4 4 0 0 0 0 0 0 1
Данные
df <- data.frame(id = c(1, 2, 1, 1:4), sku_id = c(234,345,213,233, 456, 678,657),
sku_name = c('Google', 'AMZ', 'FK', 'AB', 'JIOMART', 'CLIQ', 'AMART'))