tidyr::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)
species <chr> | island <chr> | bill_length_mm <chr> | bill_depth_mm <chr> | |
---|---|---|---|---|
Adelie | Torgersen | 39.1, 40.2, 38.9 | 18.7, 16.3, 17.1 | |
Gentoo | Biscoe | 50, 52.5 | 16 | |
Chinstrap | Dream | 52, 50.2, 46.8 | 18.1, 17.5, 16 | |
Gentoo | Biscoe | 48.7, 47.4 | 14.1, 15.8 | |
Chinstrap | Dream | 47 | 17.3 |
separate_rows(data = funcDATA, bill_length_mm, convert = TRUE)
species <chr> | island <chr> | bill_length_mm <dbl> | bill_depth_mm <chr> | |
---|---|---|---|---|
Adelie | Torgersen | 39.1 | 18.7, 16.3, 17.1 | |
Adelie | Torgersen | 40.2 | 18.7, 16.3, 17.1 | |
Adelie | Torgersen | 38.9 | 18.7, 16.3, 17.1 | |
Gentoo | Biscoe | 50.0 | 16 | |
Gentoo | Biscoe | 52.5 | 16 | |
Chinstrap | Dream | 52.0 | 18.1, 17.5, 16 | |
Chinstrap | Dream | 50.2 | 18.1, 17.5, 16 | |
Chinstrap | Dream | 46.8 | 18.1, 17.5, 16 | |
Gentoo | Biscoe | 48.7 | 14.1, 15.8 | |
Gentoo | Biscoe | 47.4 | 14.1, 15.8 |
separate_rows(data = funcDATA,
bill_length_mm, bill_depth_mm,
convert = TRUE)
species <chr> | island <chr> | bill_length_mm <dbl> | bill_depth_mm <dbl> | |
---|---|---|---|---|
Adelie | Torgersen | 39.1 | 18.7 | |
Adelie | Torgersen | 40.2 | 16.3 | |
Adelie | Torgersen | 38.9 | 17.1 | |
Gentoo | Biscoe | 50.0 | 16.0 | |
Gentoo | Biscoe | 52.5 | 16.0 | |
Chinstrap | Dream | 52.0 | 18.1 | |
Chinstrap | Dream | 50.2 | 17.5 | |
Chinstrap | Dream | 46.8 | 16.0 | |
Gentoo | Biscoe | 48.7 | 14.1 | |
Gentoo | Biscoe | 47.4 | 15.8 |
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.
LS0tCnRpdGxlOiAnRnVuY3Rpb24gb2YgdGhlIFdlZWs6IHNlcGFyYXRlX3Jvd3MnCmF1dGhvcjogIkZhcnphbmEgTmFvc2hpbiIKZGF0ZTogImByIFN5cy5EYXRlKClgIgpvdXRwdXQ6CiAgaHRtbF9kb2N1bWVudDoKICAgIGRmX3ByaW50OiBwYWdlZAogICAgY29kZV9kb3dubG9hZDogdHJ1ZQogICAgdG9jOiB5ZXMKICAgIHRvY19mbG9hdDogZmFsc2UKICAgIGNvZGVfZm9sZGluZzogc2hvdwogICAgdGhlbWU6IGNlcnVsZWFuCi0tLQoKYGBge3Igc2V0dXAsIGluY2x1ZGU9RkFMU0V9CmtuaXRyOjpvcHRzX2NodW5rJHNldChlY2hvID0gVFJVRSkKYGBgCgoKKioqICAKCiMjIyMgYHNlcGFyYXRlX3Jvd3NgCgoqKiogCgojIyMjICoqSW4gdGhpcyBkb2N1bWVudCwgSSB3aWxsIGludHJvZHVjZSB0aGUgInNlcGFyYXRlX3Jvd3MiIGZ1bmN0aW9uIGFuZCBzaG93IHdoYXQgaXQncyBmb3IuKioKCioqKiAgCgpgYGB7ciBsb2FkbGliLCBlY2hvPVQsIHJlc3VsdHM9J2hpZGUnLCBtZXNzYWdlPUYsIHdhcm5pbmc9Rn0KbGlicmFyeSh0aWR5dmVyc2UpCmxpYnJhcnkoZHBseXIpCmxpYnJhcnkocmVhZHhsKQpsaWJyYXJ5KGhlcmUpCiMgdGlkeXIgaG9zdHMgc2VwYXJhdGVfcm93cyAKbGlicmFyeSh0aWR5cikKYGBgCgoqKiogIAoKIyMjIyAqKldoYXQgaXMgaXQgZm9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