you can try using function reshape
:
to get as many columns as there are different items possible:
new_df <- reshape(df, idvar="customer_code", timevar="items", v.names="items", direction="wide")
new_df
# customer_code items.sugar items.salt items.accessories
#1 1 sugar salt <NA>
#3 2 sugar <NA> accessories
#5 3 <NA> salt <NA>
you can change the column names afterwards with colnames(new_df)[-1] <- paste0("item", 1:(ncol(new_df)-1))
another option, in case you want to get as many column as the max number of items a unique customer can have:
df_split <- split(df, df[, 1])
df_split <- lapply(df_split, reshape, idvar="customer_code", timevar="items", v.names="items", direction="wide")
max_item <- max(sapply(df_split, ncol))
df_split <- lapply(df_split, function(df){
if(ncol(df) < max_item) df <- cbind(df, matrix(NA, ncol=max_item - ncol(df)))
colnames(df)[-1] <- paste0("item", 1:(max_item-1))
return(df)
})
new_df <- do.call("rbind", df_split)
new_df
# customer_code item1 item2
#1 1 sugar salt
#2 2 sugar accessories
#3 3 salt <NA>