Старый вопрос без решения data.table
. Итак, здесь идет ...
Используя .N
library(data.table)
DT <- data.table(df)
DT[, .N, by = list(year, month)]
preg_replace
может принимать массив точно так же, как str_replace
$string = 'I have a match1 and a match3, and here\'s a match2';
$find = array('/match1/', '/match2/');
$replace = array('foo', 'bar');
$result = preg_replace($find, $replace, $string);