How to make different dataframes using split

前端 未结 2 476
长发绾君心
长发绾君心 2020-12-17 06:36

I know the split command is the easiest way to turn a df into a list of df objects, but how can they be assigned to different (seperated) dataframes?

df.List         


        
相关标签:
2条回答
  • 2020-12-17 06:55

    Hereby my solution (example with the iris dataset)

    two way:

    list_df <- split(iris, iris$Species) #split the dataset into a list of datasets based on the value of iris$Species
    list2env(list_DF, envir= .GlobalEnv) #split the list into separate datasets
    

    one way:

    list2env(split(iris, iris$Species), envir = .GlobalEnv)
    

    Or you can assign custom names for the new datasets with a for loop:

    iris_split <- split(iris, iris$Species)
    new_names <- c("one", "two", "three")
    for (i in 1:length(iris_split)) {
      assign(new_names[i], iris_split[[i]])
    
    0 讨论(0)
  • 2020-12-17 07:03

    Look at the function list2env. Try:

    list2env(split(df, df$column), envir = .GlobalEnv)
    
    0 讨论(0)
提交回复
热议问题