Quantcast

Correct use of ddply with own function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Correct use of ddply with own function

Johannes Radinger
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Correct use of ddply with own function

Tal Galili
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: [hidden email] |  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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Loading...