I can do this in python but in R I cannot find how to do conditions.
I have a matrix with colnames(mat), rownames(mat). For each column, I need to calculate sum of values if a row begins from a certain pattern. Let's say I need to sum up only the values where the row name starts from 'A'.
I tried this:
for(i in colnames(mat)) {
sum_A=0
for (j in rownames(mat)) {
sum_A<-sum(mat[ j == 'A^', i])
}
}
A
It gives me this output:
[1] 0
grepl. Please show a small reproducible example and expected ouptut.colSums(mat[starts_with(row.names(mat), "A"),])