Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

r - Split dataframe by levels of a factor and name dataframes by those levels

I want to split an existing dataframe by the levels of one of the factor variables so that the names of the split dataframes would correspond to the levels of the factor.

df <- data.frame(cbind(X = 1:10, Y = rnorm(10)), Z = sample(LETTERS[1:3], 10, replace = TRUE))

If df is the original dataframe, I want to split it into three dataframes called A, B and C, such that:

A = subset(df, Z == 'A')
B = subset(df, Z == 'B')
...

Is there an easy way to do this in one shot? I have a huge dataset and the factor variable has too many levels.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In base R, you should use the function split. And split has a default method and one for data.frame. However, I find that split.data.frame is very slow as the number of levels to split on becomes huge. That is,

# inefficient in my opinion
split(df, df$Z)

The above solution will give you the names you ask for as well directly, but will choke on large levels.

And if you're willing to trade using external packages for speed/efficiency, I'd suggest using data.table package:

require(data.table)
dt <- data.table(df)
oo <- dt[, list(list(.SD)), by = Z]$V1
names(oo) <- unique(dt$Z)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...