##### Code to generate the sample DF
cbind.fill <- function(...){
nm <- list(...)
nm <- lapply(nm, as.matrix)
n <- max(sapply(nm, nrow))
do.call(cbind, lapply(nm, function (x)
rbind(x, matrix(, n-nrow(x), ncol(x)))))
}
a <- data.frame(c("Pen","Pen","Pen","Ryu","Ryu","Ken"))
b <- data.frame(c("banana", "apple", 23, "Carrot", "grape"))
c <- data.frame(c("ryu",45,"ynwa"))
final <- data.frame(cbind.fill(a,b,c))
colnames(final) <- c("A","B","C")
A B C #This is my sample data set
1 Pen banana ryu
2 Pen apple 45
3 Pen 23 ynwa
4 Ryu Carrot <NA>
5 Ryu grape <NA>
6 Ken <NA> <NA>
################## Expected output
Output Req: I need to split the above output to 3 data frames like below:
A B C #This is my 1st data frame
1 Pen banana ryu
2 Pen apple 45
3 Pen 23 ynwa
A B C #This is my 2nd data frame
4 Ryu Carrot <NA>
5 Ryu grape <NA>
A B C #This is my 3rd data frame
6 Ken <NA> <NA>
#######I have tried this till now
> final[final=="Pen",]
#when I subset "Pen", Now i have to remove the NA
A B C
1 Pen banana ryu
2 Pen apple 45
3 Pen 23 ynwa
NA <NA> <NA> <NA>
NA.1 <NA> <NA> <NA>
NA.2 <NA> <NA> <NA>
NA.3 <NA> <NA> <NA>
> final_pen <- final[complete.cases(final=="Pen"),]
#I use complete.cases to remove NA, and this looks exactly how i want, I move onto RYU
A B C
1 Pen banana ryu
2 Pen apple 45
3 Pen 23 ynwa
> final_ryu <- final[final=="Ryu",]
#I subset Ryu
A B C
4 Ryu Carrot <NA>
5 Ryu grape <NA>
NA <NA> <NA> <NA>
NA.1 <NA> <NA> <NA>
NA.2 <NA> <NA> <NA>
NA.3 <NA> <NA> <NA>
Now when I do a complete cases here, the whole data frame vanishes because every row and column over here has a NA. The out put I expect is as below :
A B
4 Ryu Carrot
5 Ryu grape
I dont want to hardcode and subset since I would be doing this on a lot of data and splitting the big data frame using loopes into multiple data frames. Please help. This is my seond post, and im still learning to get the hang of it here. So please dont downvote if you think this is a stupid question.