[R] compute differences

Julian Burgos jmburgos at u.washington.edu
Mon Sep 21 15:02:57 CEST 2009


Here is an approach...probably there are more elegant ways

mydata=data.frame(ID=c("A","B","C","D","E"),val=c(.3,1.2,3.4,2.2,2.0))

index=expand.grid(1:nrow(mydata),1:nrow(mydata))
dif=data.frame(difference=mydata$val[index$Var2]-mydata$val[index$Var1])
rownames(dif)=paste(mydata$ID[index$Var2],"-",mydata$ID[index$Var1])     


                                                                                       
alessandro carletti wrote:
> Hi,
> I have a problem.
> I have a data frame looking like:
>
> ID val
>
> A  .3
> B  1.2
> C  3.4
> D  2.2
> E  2.0
>
> I need to CREATE the following TABLE:
>
> CASE   DIFF
>
> A-A    0
> A-B    -0.9
> A-C    -3.1
> A-D    -1.9
> A-E    -1.7
> B-A    ...
> B-B    ...
> B-C
> B-D
> B-E
> C-A
> C-B
> C-C
> C-D
> C-E
> D-A
> D-B
> D-C
> D-D
> D-E
> E-A
> E-B
> E-C
> E-D
> E-E
>
> WHERE CASE IS THE COUPLE OF ELEMENTS CONSIDEREDM AND DIFF IS THE computed DIFFERENCE between their values.
>
> Could you give me suggestions?
>
> Alessandro Carletti
>
>
>
>
> ______________________________________________
> 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.
>   


-- 
Julian Mariano Burgos
Hafrannsóknastofnunin/Marine Research Institute
Skúlagata 4, 121 Reykjavík, Iceland
Sími/Telephone : +354-5752037
Bréfsími/Telefax:  +354-5752001
Netfang/Email: julian at hafro.is, jmburgos at u.washington.edu




More information about the R-help mailing list