根据因子级别将data.frame拆分为新的data.frames [英] Split data.frame based on levels of a factor into new data.frames

查看:81
本文介绍了根据因子级别将data.frame拆分为新的data.frames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于因素水平创建单独的data.frame对象.所以,如果我有

I'm trying to create separate data.frame objects based on levels of a factor. So if I have:

df <- data.frame(
  x=rnorm(25),
  y=rnorm(25),
  g=rep(factor(LETTERS[1:5]), 5)
)

对于包含相应xy值的g的每个级别,如何将df拆分为单独的data.frame?我可以使用split(df, df$g)来获得大部分信息,但是我希望因子的每个级别都具有自己的data.frame.

How can I split df into separate data.frames for each level of g containing the corresponding x and y values? I can get most of the way there using split(df, df$g), but I'd like the each level of the factor to have its own data.frame.

做到这一点的最佳方法是什么?

What's the best way to do this?

推荐答案

我认为split确实可以满足您的要求.

I think that split does exactly what you want.

请注意,如str所示,X是数据帧的列表:

Notice that X is a list of data frames, as seen by str:

X <- split(df, df$g)
str(X)

如果您希望单个对象具有组g名称,则可以将split中的X元素分配给这些名称的对象,尽管当您仅可以索引列表split中的数据帧时,这似乎是多余的工作创建.

If you want individual object with the group g names you could assign the elements of X from split to objects of those names, though this seems like extra work when you can just index the data frames from the list split creates.

#I used lapply just to drop the third column g which is no longer needed.
Y <- lapply(seq_along(X), function(x) as.data.frame(X[[x]])[, 1:2]) 

#Assign the dataframes in the list Y to individual objects
A <- Y[[1]]
B <- Y[[2]]
C <- Y[[3]]
D <- Y[[4]]
E <- Y[[5]]

#Or use lapply with assign to assign each piece to an object all at once
lapply(seq_along(Y), function(x) {
    assign(c("A", "B", "C", "D", "E")[x], Y[[x]], envir=.GlobalEnv)
    }
)

编辑甚至比使用lapply分配给全局环境更好,请使用list2env:

Edit Or even better than using lapply to assign to the global environment use list2env:

names(Y) <- c("A", "B", "C", "D", "E")
list2env(Y, envir = .GlobalEnv)
A

这篇关于根据因子级别将data.frame拆分为新的data.frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆