Showing posts with label Data Cleaning. Show all posts
Showing posts with label Data Cleaning. Show all posts

Thursday, 23 April 2020

How do I merge Rows in R?

Q: How do I merge rows in R?

A: Tidyverse's summarize can be use. Sample below.

Requirement: Remove duplicates and merge data (rows) at the same time.

Sample data


Expected output after merging


Output when we use Excel's Remove Duplicates. Data is lost


Output using R, we get the expected output


Code:

library(tidyverse)

library(readxl)

sampleContainer<-read_excel("SampleContainers.xlsx")

cleanData<-sampleContainer %>%

  group_by(ReferenceNumber, ContainerNumber) %>%

  summarize(lastPullOutadvise=first(PullOutAdvice, order_by = PullOutAdvice),

            lastPullOutCY=first(PullOutCY, order_by = PullOutCY ))