在R dataframe中按行中的唯一因子添加行,显示按月更改的百分比

时间:2015-12-30 19:36:19

标签: r dataframe row

我有一个如下所示的数据框:它只显示MTD:12和11,多年来它只显示2014年和2015年。

testing

   MONTH         YEAR  Client   Revenue     Col1              Col2      Col3       Col4
     MTD: 12     2014     A.    3203821   651404245        477505485 73.304018  6.709496
     MTD: 11     2014     A.    2052195   484668751        334657718 69.048751  6.132221
     MTD: 11     2014     B.    1668958   533246253        305447319 57.280725  5.463978
     MTD: 12     2015     C.    1524561  3931961251        237707315  6.045515  6.413607
     MTD: 12     2015     D.    1506985   171367458        114716359 66.941740 13.136618
     MTD: 12     2014     C.    1380678   436264756        250622785 57.447406  5.508990


testing2<-split(testing, testing$Client)

sapply(testing2, ifelse(testing2$MONTH=="MTD:12", (insert row below that shows % change of every column with previous year), testing2)

然后我想要拆开并将其重新组合在一起。

1 个答案:

答案 0 :(得分:1)

也许这会有所帮助。

# sample data frame for testing code
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 just the month=12 rows
test12 <- testing[testing$MONTH=="MTD: 12", ]
test12 <- test12[order(test12$Client, test12$YEAR), ]

# define a function to calculate percent change
pctchange <- function(x) {
  L <- length(x)
  c(NA, 100 * (x[-1] - x[-L]) / x[-L])
}

# calculate percent change for all columns, by client
change <- apply(test12[, c("Revenue", "Col1", "Col2", "Col3", "Col4")], 2,
  function(y) unlist(tapply(y, test12$Client, pctchange)))
change <- data.frame(change)
names(change) <- paste0("d", names(change))
test12b <- cbind(test12[, c("MONTH", "YEAR", "Client")], change)

# merge back with monthly data
merge(testing, test12b, all=TRUE)