We can do it
i1 <- apply(m1, 1, function(x) {
v1 <- sum(unlist(lapply(strsplit(as.character(x), ""), as.numeric)))
v1 > 31 & v1 < 40})
m1[i1, , drop = FALSE]
Or
i1 <- sapply(strsplit(do.call(paste0, as.data.frame(m1)), ""),
function(x) sum(as.integer(x)))
m1[i1, , drop = FALSE]
Or we can do
f1 <- Vectorize(function(x) sum(floor(x / 10^(0:(nchar(x) - 1))) %% 10))
i1 <- rowSums(t(apply(m1, 1, f1))) %in% 31:40
m1[i1, , drop = FALSE]
data
m1 <- matrix(11:28, nrow = 3)
source
share