Answer to: Counting instances of genotype strings where order is irrelevant within locus
Score: 4
For each string, sort its characters, put them back together and count the frequencies with table.
fun <- function(x) {
lapply(x, strsplit, "") |>
unlist(recursive = FALSE) |>
lapply(sort) |>
sapply(paste, collapse = "") |>
table(useNA = "ifany")
}
fun(offspring)
#>
#> aabb aabB aaBB aAbb aAbB aABB AAbb AAbB AABB
#> 1 2 1 2 4 2 1 2 1
Created on 2026-03-03 with reprex v2.1.1
You can output as a data.frame by piping the above to as.data.frame.
fun(offspring) |> as.data.frame()
#> Var1 Freq
#> 1 aabb 1
#> 2 aabB 2
#> 3 aaBB 1
#> 4 aAbb 2
#> 5 aAbB 4
#> 6 aABB 2
#> 7 AAbb 1
#> 8 AAbB 2
#> 9 AABB 1
Created on 2026-03-03 with reprex v2.1.1
View Question ↗
Question
Parent Entity
Score: 4 • Views: 101
Site: stackoverflow
SaaS Metrics