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
337 views
in Technique[技术] by (71.8m points)

r - Automatically subset data frame by factor

Looking for help writing a function to automatically subset data frames based on the value of a column? For example,

df$x contains values a, b, c, d

I want to make separate data frames named a, b, c, d that contain all values x == 'a', or x == 'b', etc. I know several methods to do this manually but am hoping for guidance on how to automate this? Thank you!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The split function returns a list of subsetted data frames:

split(df, df$x)

EDIT:

If you want a new object for each subsetted data frame:

for (i in levels(df$x)) {
    command <- paste0(i, "<-subset(df, x=='", i, "')")
    eval(parse(text=command))
}

EDIT 2:

To split by two or more variables, a more automated solution would be to create a function that takes as input a data frame and column names with which to subset the dataframe:

create_new_df <- function (dataframe, vars) {
    # Creates a new data frame in the global environment based on names of variables in 'vars'
    split(dataframe, as.list(dataframe[, vars]), drop = TRUE) %>%
        lapply(function (subset_dataframe) {
            new_object_name <- paste(as.character(subset_dataframe[1, vars])
            # The double arrowed '<<-' creates a new object in the global environment
            command <- paste0(new_object_name, collapse="_"), "<<-subset_dataframe")
            eval(parse(text=command))
        }) %>%
        invisible()
}

This function can then be used to create new objects with any combination of variables:

variables <- c("x", "y", "z")
create_new_df(df, variables)

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

...