My example
df <- data.frame(id1 = c("a" , "b", "c"),
id2 = c("a", "a", "d"),
n1 = c(2,2,0),
n2 = c(2,1,1),
n3 = c(0,1,1),
n4 = c(0,1,1))
First, I already aggregated all rows across column like this
df <- df %>%
group_by(id2) %>%
summarise(across(c(n1,n2,n3,n4), sum, na.rm = TRUE),
.groups = "drop")
Now, but now I would like to aggregate only 2 first rows having a in column id2. How we keep the column id1 since my desire output like this. Honestly, that column is just used to compare to id2 and is quite redundant, but I really want to keep it.
id1 id2 n1 n2 n3 n4
a a 4 3 1 1
c d 0 1 1 1
Any suggestions for this?