1

I'm new to the programming world, and so I do appoligise in advance if it's really simple want I'm asking. So I need to create a UDF that will make subset's from my data frame based on the levels of one variable from that data frame.

 x<-c("a","b","c","a","b","c")
 y<-c(1,2,3,4,5,6)
 df<-data.frame(x,y)
 df
  x y
1 a 1
2 b 2
3 c 3
4 a 4
5 b 5
6 c 6

My idea was that I could store the levels into a vector, list or character, and so I would take every level and create the subset.

listlvls <- lapply(df, function(x) levels(df$x)) # levels as a list
chrlvl<-levels(df$x) # levels as a character enumeration
dflvl<-data.frame(chrlvl) # levels as a dataframe`

And I tried this with all three of the above being y but none worked:

subsetloop<-function(df,x,y){
  for(i in y){
    df[i]<-subset(df,df$x=='i')
  }
}

I want to save the subset into a new data frame with the levels name. For exemple for subset a :

adf

  x y
1 a 1
2 a 4
Val Noir
  • 19
  • 4

3 Answers3

0

We can use split to split into a list of subset of data.framess

lst1 <- split(df, df$x)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can do it more simple:

levels <- unique(as.character(df$x))
newDF <- list()
for(i in 1:length(levels))
{
  newDF[[i]] <- subset(df,df$x==levels[i])
}
Filipe Lauar
  • 434
  • 3
  • 8
0

We can also use group_split from dplyr:

library(dplyr)
df %>% group_split(x)
sm925
  • 2,648
  • 1
  • 16
  • 28