[R] increase a value by each group?

Hadley Wickham hadley at rice.edu
Mon Mar 14 18:53:22 CET 2011


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/



More information about the R-help mailing list