How do I append data frames one after the other to form another data frame?
Whether a data frame would be included or not will be decided by a criteria.
Here is an example data:
d1 <- data.frame(MyGroups =sample(LETTERS,100,replace=TRUE),
MyInt = sample(c(1:20),100,replace=TRUE))
Now, how should I choose groups (A,B,C…) from MyGroups that have mean of variable MyInt greater than 10?
I tried the following without a success. Here, I am appending the data frame into a file based on the given criteria.
require("plyr")
keepGrp <- function(df0) {
if(max(df0$MyInt < 10)) {df0 <- NULL}
write.csv(df0,'mytable.txt',append=TRUE,sep=',')
}
ddply(d1,.(MyInt),function(x) keepGrp(x))
The desired data frame should be in file mytable.txt
I am fully sure there is a better way to do what I am trying to do.
I would be happy to clarify my question if I need to do so.
I will appreciate of someone can (1) show me a feedback on improving my programming thoughts (2) give me a solution to my problem.
If I understand your question properly, you want to calculate the mean by group and only write those that meet a certain threshold to a pre-existing file. If so, why not calculate all the means at once, subset that, then write that out? Here’s a one liner that should probably be split into multiples, but I think you’ll get the point: