The main idea is to create a factor used to define the grouping for splitting. One way is by extracting the digits pattern form the provided variable Barcode using regular expression. Then we convert the obtained character vector of digits to a factor with as.factor().
We can, of course, use other regular expression techniques to get the job done, or more user friendly wrapper functions from the stringr package, like in the second example (the tidyverse-ish approach).
Example 1
A base R solution using split:
# The provided data
Barcode <- c("ABCD-1", "ABCC-1", "ABCD-2", "ABCC-2", "ABCD-3", "ABCC-3",
"ABCD-4", "ABCC-4", "ABCD-5", "ABCC-5","ABCD-6", "ABCC-6")
bar_f <- data.frame(Barcode)
factor_for_split <- regmatches(x = bar_f$Barcode,
m = regexpr(pattern = "[[:digit:]]",
text = bar_f$Barcode))
factor_for_split
#> [1] "1" "1" "2" "2" "3" "3" "4" "4" "5" "5" "6" "6"
# Create a list of 6 data frames as asked
lst <- split(x = bar_f, f = as.factor(factor_for_split))
lst
#> $`1`
#> Barcode
#> 1 ABCD-1
#> 2 ABCC-1
#>
#> $`2`
#> Barcode
#> 3 ABCD-2
#> 4 ABCC-2
#>
#> $`3`
#> Barcode
#> 5 ABCD-3
#> 6 ABCC-3
#>
#> $`4`
#> Barcode
#> 7 ABCD-4
#> 8 ABCC-4
#>
#> $`5`
#> Barcode
#> 9 ABCD-5
#> 10 ABCC-5
#>
#> $`6`
#> Barcode
#> 11 ABCD-6
#> 12 ABCC-6
# Edit names of the list
names(lst) <- paste0("df_", names(lst))
# Assign each data frame from the list to a data frame object in the global
# environment
for(name in names(lst)) {
assign(name, lst[[name]])
}
Created on 2020-02-24 by the reprex package (v0.3.0)
Example 2
And, if you prefer, here is a tidyverse-ish approach:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(stringr)
Barcode <- c("ABCD-1", "ABCC-1", "ABCD-2", "ABCC-2", "ABCD-3", "ABCC-3",
"ABCD-4", "ABCC-4", "ABCD-5", "ABCC-5","ABCD-6", "ABCC-6")
bar_f <- data.frame(Barcode)
bar_f %>%
mutate(factor_for_split = str_extract(string = Barcode,
pattern = "[[:digit:]]")) %>%
group_split(factor_for_split)
#> [[1]]
#> # A tibble: 2 x 2
#> Barcode factor_for_split
#> <fct> <chr>
#> 1 ABCD-1 1
#> 2 ABCC-1 1
#>
#> [[2]]
#> # A tibble: 2 x 2
#> Barcode factor_for_split
#> <fct> <chr>
#> 1 ABCD-2 2
#> 2 ABCC-2 2
#>
#> [[3]]
#> # A tibble: 2 x 2
#> Barcode factor_for_split
#> <fct> <chr>
#> 1 ABCD-3 3
#> 2 ABCC-3 3
#>
#> [[4]]
#> # A tibble: 2 x 2
#> Barcode factor_for_split
#> <fct> <chr>
#> 1 ABCD-4 4
#> 2 ABCC-4 4
#>
#> [[5]]
#> # A tibble: 2 x 2
#> Barcode factor_for_split
#> <fct> <chr>
#> 1 ABCD-5 5
#> 2 ABCC-5 5
#>
#> [[6]]
#> # A tibble: 2 x 2
#> Barcode factor_for_split
#> <fct> <chr>
#> 1 ABCD-6 6
#> 2 ABCC-6 6
#>
#> attr(,"ptype")
#> # A tibble: 0 x 2
#> # ... with 2 variables: Barcode <fct>, factor_for_split <chr>
names(lst) <- paste0("df_", 1:length(lst))
for(name in names(lst)) {
assign(name, lst[[name]])
Created on 2020-02-24 by the reprex package (v0.3.0)