# [R] help with combination problem

Petr Pikal petr.pikal at precheza.cz
Thu Jun 8 09:21:43 CEST 2006

```Hi

or you could try an approach of splitting original data frames and
using for cycle

> df<-matrix(sample(1:15), 3,5)
> df<-data.frame(df)
> names(df)
> names(df)[c(1,3,4)]<-c("Y1","Y2","Y3")
> df
Y1 X2 Y2 Y3 X5
1 10 15  8  3  7
2 13  6 12  2  1
3 11  5  9  4 14
> df1<-df[,grep("Y",names(df))]
> df1
Y1 Y2 Y3
1 10  8  3
2 13 12  2
3 11  9  4
> df2<-df[,-grep("Y",names(df))]
> df2
X2 X5
1 15  7
2  6  1
3  5 14
> for(i in 1:3) print(df1[,i]-df2)
X2 X5
1 -5  3
2  7 12
3  6 -3
X2 X5
1 -7  1
2  6 11
3  4 -5
X2  X5
1 -12  -4
2  -4   1
3  -1 -10

and merge resulting data frames together preferably in the cycle.

HTH
Petr

On 8 Jun 2006 at 7:50, David Hugh-Jones wrote:

Date sent:      	Thu, 8 Jun 2006 07:50:19 +0100
From:           	"David Hugh-Jones" <davidhughjones at gmail.com>
To:             	"Srinivas Iyyer" <srini_iyyer_bio at yahoo.com>
Copies to:      	r-help at stat.math.ethz.ch
Subject:        	Re: [R] help with combination problem

> hi Srinivas
>
> I'm not sure I understand: your example result has 10-9 for mu1 but 1
> for mu2, which is not equal to 11-9. I will assume you just got the
> numbers wrong. (Also J100 should be J1, no?)
>
> I am also wondering why you want what you say. It doesn't seem very
> sensible to mix the results from two data frames like this.
>
> The simplest thing is probably to do the two data frames separately,
> then intermix the rows. You can do this with for loops:
>
>
> for (col1 in paste("T", 1:14, sep="")) {
>   for (col2 in paste("N", 1:16, sep="")) result[[paste(col1, "-",
> col2, sep="")]] = df1[[col1]] - df1[[col2]] }
>
> Now repeat for data frame 2 with another data frame, e.g. result2 Then
> intermix the rows from result and result2. I'll leave you to figure
> that bit out.
>
> David
>
>
>
> On 07/06/06, Srinivas Iyyer <srini_iyyer_bio at yahoo.com> wrote:
> > hello:
> >
> > I have 3 data.frame objects.
> >
> > First df object:
> > Of dim (149,31). Columns 2:31 are marked as T1..T14
> > and N1..N16.
> >
> > Name     T1    T2    N1   T3   N2  N3  N4  T4
> > mu1      10    10    9    10   9   9   8   10
> > mu2      11    11    9    11   9   9   9   11
> > ...
> > muN      12    12    9    11   9   9   8   12
> >
> >
> >
> >
> > Second df object:
> > of Dim (50000,31). Columns 2:31 are maked as T1...T14
> > and N1..N16.
> >
> > Name     T1    T2    N1   T3   N2  N3  N4  T4
> > J1       2     3     20   2    22  21  29   3
> > J2       4     1     20   3    20  21  22   4
> > J3       3     1     33   1    31  31  33   3
> > ...
> > JX       3     2     20   2    21  22  24   2
> >
> > The column samples are identical in both first and
> > second data frames.
> >
> > Third df object:
> > of Dim (200,2).
> >
> > V1         V2
> > mu1:J1     -11
> > mu1:J100   -10.4
> > mu2:J31     11.3
> > mu2:J2      10.4
> > .....       .....
> > muN:JX     34.5
> >
> >
> >
> > I want to create a combination of Ts and Ns. Where I
> > want to subtract value of T-N in all combinations(225
> > combinations). Such as
> > T1-N1,T1-N2,T1-N3,T1-N4,T1-N5.......T14-N16
> >
> > The rows should be the row pairs from 3rd dataframe.
> >
> >
> > The final resultant matrix should look like the
> > following:
> >
> >
> >      T1-N1  T1-N2  T1-N3  T1-N4  T1-N5.......T14-N16
> > mu1   1(10-9) 1(10-9)   1    2      1            1
> > J100  -18(2-20) -20   -19  -27    -20          -29
> >
> > mu2     1     3        2     2      1            1
> > J2     -19   -21     -39    -31    -31         -28
> >
> >
> >
> > I am a beginner level in R.  I apologise for asking
> > such a big question and subsequent help. I am unable
> > to go forward as I have no idea as to how to do this.
> > Thanks
> > sri
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help