Home > Net >  Create a function/macro in R which can do more steps
Create a function/macro in R which can do more steps

Time:10-02

I have 40 different data frames with no systems in there names, like: nat69eqte, nahi_il and nahc_cpwtre. I want to create a function/macro in R which can proceed the following code in a easy way for all the data frames:

nat69eqte_wide <- spread(nat69eqte, key = time, value = values)
attr(nat69eqte_wide, "Symname") <- "nat69eqte"
lst_nat69eqte_wide <- wgdx.reshape(nat69eqte_wide, 2)

CodePudding user response:

Without any knowledge of your data, I can try to guess: the following function assumes that in each data.frame there are the columns time and values to be passed to spread. If it is not the case you can add these columns as arguments of the function.

NB spread is now deprecated, instead, use pivot_wider

myfun <- function(df){
  name <- as_label(enquo(df))
  df_wide <- spread(df, key = time, value = values)
  attr(df_wide, "Symname") <- name
  lst_df_wide <- wgdx.reshape(df_wide, 2)
  return(lst_df_wide )
}
  • Related