Vish
Vish

Reputation: 25

Find difference of same column names across different data frames in a list in R

I have a list of data frames with same column names where each dataframe corresponds to a month

June_2018 <- data.frame(Features=c("abc","def","ghi","jkl"), Metric1=c(100,200,250,450), Metric2=c(1000,2000,5000,6000))
July_2018 <- data.frame(Features=c("abc","def","ghi","jkl"), Metric1=c(140,250,125,400), Metric2=c(2000,3000,2000,3000))
Aug_2018 <- data.frame(Features=c("abc","def","ghi","jkl"), Metric1=c(200,150,250,600), Metric2=c(1500,2000,4000,2000))
Sep_2018 <- data.frame(Features=c("abc","def","ghi","jkl"), Metric1=c(500,500,1000,100), Metric2=c(500,4000,6000,8000))
lst1 <- list(Aug_2018,June_2018,July_2018,Sep_2018)
names(lst1) <- c("Aug_2018","June_2018","July_2018","Sep_2018")

I intend to create a new column in each of the data frames in the list named Percent_Change_Metric1 and Percent_Change_Metric2 by doing below calculation

for (i in names(lst1)){
lst1[[i]]$Percent_Change_Metric1 <- ((lst1[[i+1]]$Metric1-lst1[[i]]$Metric1)*100/lst1[[i]]$Metric1)
lst1[[i]]$Percent_Change_Metric2 <- ((lst1[[i+1]]$Metric2-lst1[[i]]$Metric2)*100/lst1[[i]]$Metric2)

}

However, obviously the i in for loop is against the names(lst1) and wouldn't work

Also, the dataframes in my list in random order and not ordered by month-year. So the calculation to subtract successive dataframes' columns isn't entirely accurate.

Please advise

  1. How I go about with adding the Percent_change_Metric1 and Percent_change_Metric2
  2. How to choose the dataframe corresponding to next month to arrive at the correct Percent_Change

    Thanks for the guidance

Upvotes: 1

Views: 77

Answers (1)

akrun
akrun

Reputation: 887153

Here is one option with base R

lst1[-length(lst1)] <- Map(function(x, y) 
     transform(y, Percent_Change_Metric1 = (x$Metric1 - Metric1) * 100/Metric1, 
     Percent_Change_Metric2 = (x$Metric2 - Metric2) * 100/Metric2),
          lst1[-1], lst1[-length(lst1)])

Upvotes: 1

Related Questions