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

From: Hadley Wickham <hadley_at_rice.edu>
Date: Mon, 14 Mar 2011 12:53:22 -0500

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.
Received on Mon 14 Mar 2011 - 17:56:31 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:20: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