如何根据表中的名称创建过滤的data.frames?

时间:2017-01-12 14:33:46

标签: r dplyr

我希望使用names(table(data))生成变量列表以应用过滤器来对data.frame进行分组,但不知道如何为data.frame名称生成唯一值保存它们。

Type <- c("A","A","A","A","A","A","A","A","A",
      "B","B","B","B","B","B","B","B",
      "C","C","C","C","C","C","C",
      "AB","AB","AB","AB","AB")
Type <- as.data.frame(Type)

这就是我如何生成我想要的唯一值:

type_names <- names(table(droplevels(Type$Type)))

这是我不太完善的功能:

library(dplyr)
NamedTypes <- function(data, nametype) {
  data %>% 
    filter(Type == nametype) %>% 
    a <- paste0(nametype, "_type")
}

那我觉得我怎么运行这个功能:

lapply(type_names, function(x) NamedTypes(Type, x))

1 个答案:

答案 0 :(得分:3)

您可以使用split创建列表,list2env将数据框分配给您的全局环境

list2env(split(Type, Type$Type), globalenv())