Hi Johannes , Try this:
var1 <- rep(c("a","b"),c(100,100)) var2 <- runif(200,1,50) df.test <- data.frame(var1,var2) fn <- function(x){ x <- x$var2 x1 <- sort(x) x2 <- seq(length(x)) x3 <- x2/max(x2) df <- data.frame(x1,x2,x3) df } require(plyr) ddply(df.test,.(var1),fn) I think it should do what you've asked. ----------------Contact Details:------------------------------------------------------- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Sat, May 5, 2012 at 12:51 PM, Johannes Radinger <jradin...@gmx.at> wrote: > Hi, > > I am really confused how ddply work, so maybe you can help me. > > I created a function that sorts a vector etc. > > fn <- function(x){ > x1 <- sort(x) > x2 <- seq(length(x)) > x3 <- x2/max(x2) > df <- data.frame(x1,x2,x3) > df > } > > Probably this is not the best form of the function, but at least it > produces what I want (data to plot a cumulative count curve). > This function works on a single vector but I have a melted dataframe like: > > var1 <- rep(c("a","b"),c(100,100)) > var2 <- runif(200,1,50) > df.test <- data.frame(var1,var2) > > ..and I want to apply that function on var2 but splitted by the variable > var1. I think this might be a case for ddply... > > anything like: ddply(df.test,.(var1),fn(var2))... > maybe someone know how to do that (modifying my function and applying it > on a splitted dataframe). > > Best regards, > > Johannes > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.