Create new objects applying a loop

I want to create new dataframes by extracting each column from the base dataframe data_model applying the split function and saving each new transformed variable in a new dataframe.

I have applied the following code:

for (i in c("l_inf","l_tax","l_fd","l_gdp")) {
new_df <- paste0("df_",i)
   new_df <- data.frame(split(data_model$paste0(i),data_model$code,drop = TRUE))
}

it returns the following error to me:

Error in data_modelo$paste0(i) : attempt to apply non-function

help me!!

Hi @marcelo93, it would be very helpful if you provided a reproducible example! I think the problem is that you are trying to extract a column from data_model using the $ operator, which expects a column name. You are providing a function, paste0(i). I think you can just index the column using i:

for (i in c("l_inf", "l_tax", "l_fd", "l_gdp")) {
new_df <- paste0("df_", i)
   new_df <- data.frame(split(data_model$i, data_model$code, drop = TRUE))
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.