tidyr::separate_rows()

Function of the Week: separate_rows

separate_rows


In this document, I will introduce the “separate_rows” function and show what it’s for.


library(tidyverse)
library(dplyr)
library(readxl)
library(here)
# tidyr hosts separate_rows 
library(tidyr)

What is it for?


This function separates multiple values recorded for an observation for one variable. For example, three bill length is recorded for one observation below, ‘separate_rows’ function will separate them into three rows.


funcDATA = read_excel(here('data/function.xlsx'), 
                      sheet=1, 
                      na="NA")
slice(funcDATA)

separate_rows(data = funcDATA, bill_length_mm, convert = TRUE)

We can also do it for multiple variables having more than one data point for one variable.


separate_rows(data = funcDATA, 
              bill_length_mm, bill_depth_mm, 
              convert = TRUE)

Is it helpful?


I think it is a great tool to separate multiple values from a variable into separate rows when working on a dataset with this problem.


LS0tCnRpdGxlOiAnRnVuY3Rpb24gb2YgdGhlIFdlZWs6IHNlcGFyYXRlX3Jvd3MnCmF1dGhvcjogIkZhcnphbmEgTmFvc2hpbiIKZGF0ZTogImByIFN5cy5EYXRlKClgIgpvdXRwdXQ6CiAgaHRtbF9kb2N1bWVudDoKICAgIGRmX3ByaW50OiBwYWdlZAogICAgY29kZV9kb3dubG9hZDogdHJ1ZQogICAgdG9jOiB5ZXMKICAgIHRvY19mbG9hdDogZmFsc2UKICAgIGNvZGVfZm9sZGluZzogc2hvdwogICAgdGhlbWU6IGNlcnVsZWFuCi0tLQoKYGBge3Igc2V0dXAsIGluY2x1ZGU9RkFMU0V9CmtuaXRyOjpvcHRzX2NodW5rJHNldChlY2hvID0gVFJVRSkKYGBgCgoKKioqICAKCiMjIyMgYHNlcGFyYXRlX3Jvd3NgCgoqKiogCgojIyMjICoqSW4gdGhpcyBkb2N1bWVudCwgSSB3aWxsIGludHJvZHVjZSB0aGUgInNlcGFyYXRlX3Jvd3MiIGZ1bmN0aW9uIGFuZCBzaG93IHdoYXQgaXQncyBmb3IuKioKCioqKiAgCgpgYGB7ciBsb2FkbGliLCBlY2hvPVQsIHJlc3VsdHM9J2hpZGUnLCBtZXNzYWdlPUYsIHdhcm5pbm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