continuation question: add row in r dataframe unique factor in column showing percent change month testing <- data.frame( month = c("mtd: 12", "mtd: 12", "mtd: 11", "mtd: 12", "mtd: 12", "mtd: 12"), year = c(2012, 2013, 2014, 2015, 2013, 2014), client = c("a.", "a.", "a.", "b.", "b.", "b."), revenue = c(320, 205, 166l, 152, 150, 138), col1 = c(651, 485, 533, 3932, 171, 436), col2 = c(478, 335, 305, 238, 115, 251), col3 = c(73, 69, 57, 6, 67, 57), col4 = c(6.7, 6.1, 5.5, 6.4, 13.1, 5.5) ) # subset month=12 rows test12 <- testing[testing$month=="mtd: 12", ] test12 <- test12[order(test12$client, test12$year), ] # define function calculate percent change pctchange <- function(x) { l <- length(x) c(na, 100 * (x[-1] - x[-l]) / x[-l]) } # calculate percent change columns, client change <- apply(test12[, c("...