Re: [R] increase a value by each group?

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Mon, 14 Mar 2011 11:02:28 -0700

I think you meant to use Depth as the grouping factor rather than s_name:

> ddply(my_data, .(s_name), function(x){

+         x$Im_looking <- x$Depth + as.numeric(factor(x$s_name)) / 100
+         x
+ })

   Depth s_name index Im_looking

1   3852 Site_1   144    3852.01
2   3852 Site_1   144    3852.01
3   3852 Site_1   144    3852.01
4   3852 site_A   145    3852.01
5   3852 site_A   145    3852.01
6   3852 site_A   145    3852.01
7   3852 site_A   145    3852.01
8   3852 site_B   147    3852.01
9   3852 site_B   147    3852.01
10  3852 site_B   147    3852.01
11  3852 site_B   147    3852.01
12 54962 site_C    27   54962.01
13 54962 site_C    27   54962.01
14 54962 Site_D   217   54962.01
15 54962 Site_D   217   54962.01
16 54962 Site_D   217   54962.01
17 54962 Site_D   217   54962.01

> ddply(my_data, .(Depth), function(x){
+ x$Im_looking <- x$Depth + as.numeric(factor(x$s_name)) / 100 + x

+ })

   Depth s_name index Im_looking

1   3852 Site_1   144    3852.01
2   3852 Site_1   144    3852.01
3   3852 Site_1   144    3852.01
4   3852 site_A   145    3852.02
5   3852 site_A   145    3852.02
6   3852 site_A   145    3852.02
7   3852 site_A   145    3852.02
8   3852 site_B   147    3852.03
9   3852 site_B   147    3852.03
10  3852 site_B   147    3852.03
11  3852 site_B   147    3852.03
12 54962 site_C    27   54962.01
13 54962 site_C    27   54962.01
14 54962 Site_D   217   54962.02
15 54962 Site_D   217   54962.02
16 54962 Site_D   217   54962.02
17 54962 Site_D   217   54962.02

Dennis

On Mon, Mar 14, 2011 at 10:53 AM, Hadley Wickham <hadley_at_rice.edu> wrote:

> On Mon, Mar 14, 2011 at 9:59 AM, ONKELINX, Thierry
> <Thierry.ONKELINX_at_inbo.be> wrote:
> > Something like this?
> >
> > my_data=read.table("clipboard", header=TRUE)
> > my_data$s_name <- factor(my_data$s_name)
> > library(plyr)
> > ddply(my_data, .(s_name), function(x){
> > x$Im_looking <- x$Depth + as.numeric(x$s_name) / 100
> > x
> > })

>
>

> I think you need factor in there:
>

> ddply(my_data, .(s_name), function(x){
> x$Im_looking <- x$Depth + as.numeric(factor(x$s_name)) / 100
> x
> })
>

> or with transform:
>

> ddply(my_data, "s_name", transform,
> Im_looking = Depth + as.numeric(factor(s_name)) / 100)
>

> Hadley
>

> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>

> ______________________________________________
> R-help_at_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_at_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. Received on Mon 14 Mar 2011 - 18:05:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Mar 2011 - 18:40:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive