Thanks Petr! It is good to see multiple solutions to the same problem.
Best,
Jude
-Original Message-
From: Petr PIKAL [mailto:petr.pi...@precheza.cz]
Sent: Wednesday, September 23, 2009 10:59 AM
To: Ryan, Jude
Cc: alxmil...@yahoo.it; r-help@r-project.org
Subject: Re: [R] compute
Hi
You can use outer. If your data are in data frame test then
DIFF <- as.vector(t(outer(test$val, test$val, "-")))
returns a vector, You just need to add suitable names to rows.
CASE <- as.vector(t(outer(test$ID, test$ID, paste, sep="-")))
data.frame(CASE, DIFF)
will put it together.
Regard
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
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],"-
Try this:
data.frame(ID = apply(expand.grid(x$ID, x$ID), 1, paste, collapse =
'-'), diff = c(outer(x$val, x$val, '-')))
On Mon, Sep 21, 2009 at 9:39 AM, 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
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
6 matches
Mail list logo