I am trying to find a way to do 2 things.
Take a dataset and separate it into individual datasets based on its value in a specific variable.
Run each of those datasets through a function using a for() or other statement.
This is a very simple version of what I want I am doing now.
x <- c("Red", "Red", "Red", "Orange", "Blue", "Orange", "Red", "Blue")
y <- c(2,3,1,4,2,6,4,3)
z <- df(x,y)
Red <- z[z$x == "Red",]
Blue <- z[z$x == "Blue",]
Orange <- z[z$x == "Orange",]
Function1 <- function(x){
a <- mean(x$y)
return(a)}
Mean1 <- Function1(Red)
Mean2 <- Function1(Blue)
Mean3 <- Function1(Orange)
Table <- rbind(Mean1, Mean2)
Table <- rbind(Table, Mean3)
Table
I would like to simplify this to automatically create a dataset for each unique 'color', and then run each 'color' dataset through a function.
Appreciate any feedback, thanks!