Home > Net >  Applying own function
Applying own function

Time:01-06

I am trying to implement my own function. The function works with three arguments that need to be changed for each subsequent column.

# Data
library(dplyr)

df<-data.frame(  
              Year=c("2000","2001","2002","2003","2004","2005","2006","2007","2008","2009"),
              Sales=c(100,200,300,400,500,600,100,300,200,200),
              # Store,Mall and Grocery
              Store=c(100,400,300,800,900,400,800,400,300,100),
              Mall=c(100,600,300,200,200,300,200,500,200,400),
              Grocery=c(100,600,300,200,200,300,200,500,200,400),
              # Building   Store,Mall and Grocery
              Building_Store=c(100,200,300,400,500,600,100,300,200,400),
              Building_Mall=c(100,400,300,800,900,400,800,400,300,600),
              Building_Grocery=c(100,600,300,200,200,300,200,500,200,400))
  
# Own function
my_function <- function(x,y,z){((x-(y*lag(z))))}

This function I applied this with dplyr and code you can see below

estimation<-mutate(df,
                   df_Store=my_function(Store,Sales,Building_Store),
                   df_Mall=my_function(Mall,Sales,Building_Mall),
                   df_Grocery=my_function(Grocery,Sales,Building_Grocery))
                     

In this way, I applied this function by manually changing arguments in the function. Results you can see below

enter image description here

Otherwise, in practice, I have a huge set with dozens of such arguments and it is not possible to enter them all manually.

Can someone help me by applying the map function to automatically get the results shown in the above table?

CodePudding user response:

You can try this:

library(dplyr)
library(tidyr)

rename(df, 
       Value_Store=Store,
       Value_Mall=Mall,
       Value_Grocery=Grocery) %>%
  pivot_longer(-c(Year, Sales), names_to=c(".value", "name"), names_sep="_") %>%
  mutate(df=my_function(Value, Sales, Building)) %>%
  pivot_wider(values_from=c(Value, Building, df)) %>%
  select(Year, Sales, starts_with('df'))

# A tibble: 10 × 5
   Year  Sales df_Store df_Mall df_Grocery
   <chr> <dbl>    <dbl>   <dbl>      <dbl>
 1 2000    100       NA   -9900      -9900
 2 2001    200   -19600  -39400     -79400
 3 2002    300  -179700  -89700     -89700
 4 2003    400  -119200 -159800    -319800
 5 2004    500   -99100 -249800    -449800
 6 2005    600  -119600 -359700    -239700
 7 2006    100   -29200   -9800     -79800
 8 2007    300   -59600  -89500    -119500
 9 2008    200   -99700  -39800     -59800
10 2009    200   -39900  -79600    -119600
  • Related