r - Reordering a list by element in list and remove specified rows in list -
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("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 monthly data merge(testing, test12b, all=true)
so after running code list has been splitted client.
i want run following code remove 2nd row if number of rows factor (the client) greater 2.
i tried , didn't work:
testing<-ifelse(length(splitresult)>2,splitresult[-2,],splitresult)
ultimate goal out of of this:
1) percent change of last year previous year , don't show inbetween stuff na previous year. if new client want na there specify new client. why tried out code above didn't work.
2) want reorder clients in split revenue in mtd: 12 2014.
splitlist[order(sapply(splitlist, function(x) (x[["revenue"]])))]
(didn't work: assume splitlist name of list)
if can me either question extremely helpful. thanks!
i think plyr
package here. example, instead of last line of code use ifelse
, try
library(plyr) out = ddply(splitresult, "client", function(x){ if(dim(x)[1] > 2) x = x[-2,] return(x) })
here, x
client-specific data frame, , out
result of combining rows of bunch of client-specific data frames.
you might check out lubridate
, make dates , times easier deal with. mentioned in comments, dplyr
helpful, rest of "hadleyverse" of packages cleaning , plotting data. solutions questions 1 , 2, along whole process of cleaning , summarizing, cleaner , easier right tools.
Comments
Post a Comment