Split a Dataframe
split_by(data, col, drop = FALSE, sep = ".", lex.order = FALSE)
data | A dataframe or tibble. |
---|---|
col | Target column. |
drop | logical indicating if levels that do not occur should be dropped
(if |
sep | character string, passed to |
lex.order | logical, passed to |
library(tidyverse) test_data <- tibble( Group = sample(c("Apple", "Pear"), size = 10, replace = TRUE), A = sample(c(NA_integer_, 1:3), size = 10, replace = TRUE), B = sample(c(NA_integer_, 4:6), size = 10, replace = TRUE) ) # Split split_by(test_data, col = Group)#> $Apple #> # A tibble: 4 x 3 #> Group A B #> <chr> <int> <int> #> 1 Apple 2 5 #> 2 Apple 3 5 #> 3 Apple NA NA #> 4 Apple 1 4 #> #> $Pear #> # A tibble: 6 x 3 #> Group A B #> <chr> <int> <int> #> 1 Pear 1 NA #> 2 Pear 2 NA #> 3 Pear 3 NA #> 4 Pear 1 5 #> 5 Pear 2 5 #> 6 Pear 1 NA #>#> $Apple #> # A tibble: 4 x 2 #> A B #> <int> <int> #> 1 2 5 #> 2 3 5 #> 3 NA NA #> 4 1 4 #> #> $Pear #> # A tibble: 6 x 2 #> A B #> <int> <int> #> 1 1 NA #> 2 2 NA #> 3 3 NA #> 4 1 5 #> 5 2 5 #> 6 1 NA #>