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

Jul_biologyGrad julleeyaw at yahoo.ca
Wed Sep 19 06:47:14 CEST 2012


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<-c(1,1,1,2,2,2,3,3,3)
Mass<-c(3,3.1,3.4,4,4.3,4.4,3,3.2,3.5)
Time<-c(1,2,3,1,2,3,1,2,3)
mydata<-as.data.frame(cbind(Sample,Time,Mass))

## I split the dataset by Sample and then calculate the difference between
mass at time 3 and mass at time 2 for each Sample; then use the merge
function to attach this data to my initial dataset

sp<-split(mydata,mydata$Sample)
y<-rbind(lapply(sp,function(x){Gain<-x$Mass[x$Time==3]-x$Mass[x$Time==2]}))

## note here that as I modification to some of the suggestions posted, I
wanted a way to specifically call "mass at time 3" etc. for each sample
rather than relying on the position of such data within each split/Sample
(hence allowing me to deal with samples that may have the Time/Mass data
input in a different order

# some massaging of the results
u<-t(y)
s<-data.frame(Sample=row.names(u),Gain2_3=u)
fulldata<-merge(mydata,s)

## as I wished to export the data in the end using write.csv, I had to
covert "list" data into "numeric" in the final dataframe

fulldata$Gain<-as.numeric(fulldata$Gain2_3) 
fulldata$Gain2_3<-NULL

Thanks again everyone!




--
View this message in context: http://r.789695.n4.nabble.com/help-with-calculation-from-dataframe-with-multiple-entries-per-sample-tp4643434p4643581.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list