[英]How to create filtered data.frames based on names from a table?
I'm looking to use names(table(data))
to generate a list of variables to apply a filter to subset a data.frame but don't know how to generate unique values for the data.frame names to save them. 我正在寻找使用
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)
This is how I'm generating the unique values I want: 这就是我生成所需的唯一值的方式:
type_names <- names(table(droplevels(Type$Type)))
This is my not-quite-there function: 这是我的不完全功能:
library(dplyr)
NamedTypes <- function(data, nametype) {
data %>%
filter(Type == nametype) %>%
a <- paste0(nametype, "_type")
}
Then how I think I'd run the function: 然后我认为我会运行该函数:
lapply(type_names, function(x) NamedTypes(Type, x))
您可以使用split
创建一个列表,并使用list2env
将数据帧分配给全局环境
list2env(split(Type, Type$Type), globalenv())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.