Home > Software engineering >  Merging strings in R
Merging strings in R

Time:11-16

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

CodePudding user response:

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)

CodePudding user response:

I pivot data this way:

library(tidyverse)
df <- data.frame(Transaction=c(1,2,2),
                 Item=c("Apple", "Banana", "Coconut"))
df
df <- df %>% 
  group_by(Transaction) %>% 
  mutate(ItemNumber=paste0("Item", row_number()))
df
df <- df %>% 
  pivot_wider(names_from = ItemNumber, 
              values_from = Item)
df
#  Transaction Item1  Item2  
#1           1 Apple  NA     
#2           2 Banana Coconut
  •  Tags:  
  • r
  • Related