Hi there,
The pivot functions are very powerful yet indeed sometimes tricky to wrap your head around. In your example however, you can't solve the issue by just using one pivot function, rather you want to carry out a summary across the columns per group. So that would translate to this
library(tidyverse)
myData = data.frame(
KRAS = c("M", "NM", "NM", "M"),
EGFR = c("M", "NM", "M", "NM"),
TP53 = c("NM", "M", "M", "NM"),
group = c("1", "2", "3", "1")
)
myData %>% group_by(group) %>%
summarise(across(everything(), function(x) sum(x == "M")))
#> # A tibble: 3 x 4
#> group KRAS EGFR TP53
#> <chr> <int> <int> <int>
#> 1 1 2 1 0
#> 2 2 0 0 1
#> 3 3 0 1 1
Created on 2022-02-25 by the reprex package (v2.0.1)
By using the across() function we can summarise over all columns at once (by group) and get the numbers you need.
Hope this helps,
PJ