Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-19 Thread R. Michael Weylandt
On Wednesday, September 19, 2012, Jul_biologyGrad wrote: > Thanks everyone for the help! I pulled together a bunch of your suggestions > to get the result that I needed. I'm posting my final code below. Probably > not the most efficient way of doing things but gets the job done in a way > that a n

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-18 Thread Jul_biologyGrad
Thanks everyone for the help! I pulled together a bunch of your suggestions to get the result that I needed. I'm posting my final code below. Probably not the most efficient way of doing things but gets the job done in a way that a newbie can understand! ##Here again is the example dataset Sample

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-18 Thread William Dunlap
e wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of arun > Sent: Monday, September 17, 2012 8:12 PM > To: Julie Lee-Yaw > Cc: R help > Subject: Re: [R] help with calculation from dataf

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-17 Thread David Winsemius
On Sep 17, 2012, at 7:28 PM, arun wrote: > HI, > Try this: > mydata$Gain<-rep(tapply(mydata$Mass,mydata$Sample,FUN=function(x) > (x[3]-x[2])),each=length(unique(mydata$Sample))) > mydata > # Sample Time Mass Gain > #1 11 3.0 0.3 > #2 12 3.1 0.3 > #3 13 3.4 0.3

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-17 Thread arun
HI, Modified version of my earlier solution: res1<-tapply(mydata$Mass,mydata$Sample,FUN=function(x) (x[3]-x[2])) res2<-data.frame(Sample=names(res1),Gain2_3=res1)  merge(mydata,res2) #Sample Time Mass Gain2_3 #1  1    1  3.0 0.3 #2  1    2  3.1 0.3 #3  1    3  3.4 0.3 #4   

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-17 Thread arun
HI, Try this:  mydata$Gain<-rep(tapply(mydata$Mass,mydata$Sample,FUN=function(x) (x[3]-x[2])),each=length(unique(mydata$Sample)))  mydata #  Sample Time Mass Gain #1  1    1  3.0  0.3 #2  1    2  3.1  0.3 #3  1    3  3.4  0.3 #4  2    1  4.0  0.1 #5  2    2  4.3  0.1 #6  2 

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-17 Thread Rui Barradas
Or diff(x[2:3]) Rui Barradas Em 18-09-2012 01:05, David Winsemius escreveu: On Sep 17, 2012, at 5:00 PM, David Winsemius wrote: On Sep 17, 2012, at 4:15 PM, Julie Lee-Yaw wrote: Hi I have a dataframe similar to: Sample<-c(1,1,1,2,2,2,3,3,3) Time<-c(1,2,3,1,2,3,1,2,3) Mass<-c(3,3.1,3.4,4,4

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-17 Thread David Winsemius
On Sep 17, 2012, at 5:00 PM, David Winsemius wrote: > > On Sep 17, 2012, at 4:15 PM, Julie Lee-Yaw wrote: > >> Hi >> >> I have a dataframe similar to: >> >>> Sample<-c(1,1,1,2,2,2,3,3,3) >> >>> Time<-c(1,2,3,1,2,3,1,2,3) >> >>> Mass<-c(3,3.1,3.4,4,4.3,4.4,3,3.2,3.5) >> >>> mydata<-as.data

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-17 Thread David Winsemius
On Sep 17, 2012, at 4:15 PM, Julie Lee-Yaw wrote: > Hi > > I have a dataframe similar to: > >> Sample<-c(1,1,1,2,2,2,3,3,3) > >> Time<-c(1,2,3,1,2,3,1,2,3) > >> Mass<-c(3,3.1,3.4,4,4.3,4.4,3,3.2,3.5) > >> mydata<-as.data.frame(cbind(Sample,Time,Mass)) > Please tell me where you learned tha

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-17 Thread Rui Barradas
Hello, Try the following. sp <- split(mydata, mydata$Sample) do.call(rbind, lapply(sp, function(x){x$Gain <- x$Mass[3] - x$Mass[2]; x})) Hope this helps, Rui Barradas Em 18-09-2012 00:15, Julie Lee-Yaw escreveu: > Hi > > I have a dataframe similar to: > >> Sample<-c(1,1,1,2,2,2,3,3,3) >> Time<-

Re: [R] help with calculation from dataframe with multiple entries per sample

2012-09-17 Thread Phil Spector
Julie - Since the apply functions operate on one row at a time, they can't do what you want. I think the easiest way to solve your problem is to reshape the data set, and merge it back with the original: dd = data.frame(Sample=c(1,1,1,2,2,2,3,3,3), + Time=c(1,2,3,1,2,3,1,