0

Does anyone know if there are any ways I can turn dataset from the left side to the right side in excel macro or other programming tools?

enter image description here

Use R code to manipulate dataset on the left side to the one at the right side

MrFlick
  • 195,160
  • 17
  • 277
  • 295
J-20
  • 1
  • This may be useful: https://stackoverflow.com/questions/15933958/ – Barranka Nov 15 '22 at 14:39
  • 2
    What exactly is the desired output? Do you want a data.frame? Is the "Item" column supposed to be multiple columns or just one? I'm not sure how to interpret your image in terms of R objects. – MrFlick Nov 15 '22 at 14:42

2 Answers2

0

This doesn't exactly match what you are looking for, but further data analysis might be easier formatted this way

library(tidyverse)

df = read_csv("Downloads/test.csv")

df = df %>% group_by(Transaction, Item) %>% summarize(count_value = n())

result = df %>% pivot_wider(names_from = "Item", values_from = "count_value", values_fill = 0)

0

I pivot data this way:

library(tidyverse)
df <- data.frame(Transaction=c(1,2,2),
                 Item=c("Apple", "Banana", "Coconut"))

df %>% 
  group_by(Transaction) %>% 
  mutate(ItemNumber = paste0("Item", row_number())) %>% 
  pivot_wider(names_from = ItemNumber, 
              values_from = Item)

#  Transaction Item1  Item2  
#1           1 Apple  NA     
#2           2 Banana Coconut
M.Viking
  • 5,067
  • 4
  • 17
  • 33