azmath
azmath

Reputation: 97

How to automatically create a growth rate table for every corresponding column in a different table in R?

Imagine there are 100 columns, some of them categorical and most of them numerical. One of the column is Year.

I want to create a different dataset which shows the growth rate for each of the column. For example, if column one is Sales, I need sales growth rate; column two is income, I need corresponding Income growth rate. Please note there are 100 columns, so I am not looking to manually check growth rate for each column. I am assuming some sort of for loop is required.

#Here is an example for one column
           Diff_year <- growthFund$Year - lag(growthFund$Year)
           Diff_growth <- Sales - lag(Sales)
           Rate_percent <- (Diff_growth / Diff_year)/Sales * 100

Upvotes: 2

Views: 342

Answers (1)

akrun
akrun

Reputation: 887048

If it is needed only for numeric variables, apply the function only across the columns that are numeric with is.numeric to find those columns

library(dplyr)
growthFund %>%
     mutate(across(where(is.numeric), Growth, .names = "{col}_new"))

Upvotes: 1

Related Questions