Search code examples
rdataframesplitlapplynames

split dataframe by factor and name new df by the factor and addidtional description like "new_dataframe(factor)"


I need to split my df into several new df by a factor via loop. Problem, the factor consist of numbers, and the new df are called "1" "2" and so on, this makes it hard to call them for the next pice of code,... Any help how I can rename/name the new df´s. like new_df_1, new_df_2?

what I have so far:

new_df<- split(df, df$cluster)
new_names <- as.character(unique(df$cluster))
for (i in 1:length(new_df))
{assign(new_names[i],new_df[[i]])}

I also tried lapplybut was only able to save, not to make a df in the Global Environment, as I actually don´t need it saved for later.

new_df<- split(df, df$cluster)
lapply(names(new_df),function(nm)
write.csv(new_df[[nm]],paste("new_df",nm,".csv")))

It works, but makes a file: new_df 1.csv

Thanks for any suggestions!


Solution

  • cluster_list <- split(df, f = df$clust)

    builds the cluster list, to manipulate the list use: lapply.