rloopssubsetiterated-function

How to create a loop which creates multiple subset dataframes from a larger data frame?


I am trying to create code in R that will instantly recognize the value of a certain column, find all rows with that value, and extract the data from of all of those rows including all other columns intersecting those rows in a new data frame. I want this to repeat for every distinct value inside the base column. for instance:

mydata <- data.frame(x = c(1,2,3), y = c('a','b','c'), z = c('red','red','yellow'))
colors <- list(mydata$z)
for (i in 1:length(colors)) {
   assign(paste0("mydata_",i), subset(mydata, z == colors[[i]]))
}

this was my latest attempt but can't get it to work. the goal is to have in this example 2 new dataframes called "mydata_red" and "my_data_yellow". Each will only contain the matching rows


Solution

  • Your code works fine. Just remove list so you create a vector of color names and not a list. If you only want distinct values, use unique.

    mydata <- data.frame(x = c(1,2,3), y = c('a','b','c'), z = c('red','red','yellow'))
    
    colors <- unique(mydata$z)
    
    for (i in 1:length(colors)) {
        assign(paste0("mydata_",i), subset(mydata, z == colors[[i]]))
        }