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