[R] Correct use of ddply with own function

Johannes Radinger jradinger at gmx.at
Sat May 5 11:51:36 CEST 2012


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



More information about the R-help mailing list