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 <- split(df, df$column)
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 <- split(df, df$column)
看功能list2env
。尝试:
list2env(split(df, df$column), envir = .GlobalEnv)
特此我的解决方案(以 iris 数据集为例)
两种方式:
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
单程:
list2env(split(iris, iris$Species), envir = .GlobalEnv)
for
或者您可以使用循环为新数据集分配自定义名称:
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]])