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 <had...@rice.edu> wrote: > On Mon, Mar 14, 2011 at 9:59 AM, ONKELINX, Thierry > <thierry.onkel...@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@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@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.