[R] Help create a loopto conduct multiple pairwise operations

jim holtman jholtman at gmail.com
Fri Sep 21 02:52:05 CEST 2007


You can put the dataframes in a list and then use 'combn' to determine
the possible pairwise combinations and then use this in an lapply to
compute the products which is also in a list:

>
> X.alleles      <- c(1,5,6,7,8)
> X.Freq        <- c(0.35, 0.15, 0.05 , 0.10, 0.35)
> Loc1          <- cbind( X.alleles,X.Freq)
> X               <- data.frame(Loc1)
>
> #creation of data frame Y
>
> Y.alleles  <- c(1,4,6,8)
> Y.Freq     <- c(0.35, 0.35, 0.10, 0.20 )
> Loc2        <- cbind(Y.alleles, Y.Freq)
> Y             <- data.frame (Loc2)
>
> # creation of data frame Z
>
> Z.alleles  <- c(1,4,5,6,8)
> Z.Freq     <- c(0.35, 0.35, 0.05, 0.05, 0.20)
> Loc3       <- cbind(Z.alleles, Z.Freq)
> Z            <- data.frame (Loc3)
>
> X
  X.alleles X.Freq
1         1   0.35
2         5   0.15
3         6   0.05
4         7   0.10
5         8   0.35
> Y
  Y.alleles Y.Freq
1         1   0.35
2         4   0.35
3         6   0.10
4         8   0.20
> Z
  Z.alleles Z.Freq
1         1   0.35
2         4   0.35
3         5   0.05
4         6   0.05
5         8   0.20
> # create a list of the dataframes you want to multiply
> dfList <- list(X, Y, Z)
> # get possible pairwise matches
> combo <- combn(length(dfList), 2)
> result <- lapply(seq(ncol(combo)), function(.col){
+     dfList[[combo[1, .col]]][, 2] %o% dfList[[combo[2, .col]]][, 2]
+ })
>
> result
[[1]]
       [,1]   [,2]  [,3] [,4]
[1,] 0.1225 0.1225 0.035 0.07
[2,] 0.0525 0.0525 0.015 0.03
[3,] 0.0175 0.0175 0.005 0.01
[4,] 0.0350 0.0350 0.010 0.02
[5,] 0.1225 0.1225 0.035 0.07

[[2]]
       [,1]   [,2]   [,3]   [,4] [,5]
[1,] 0.1225 0.1225 0.0175 0.0175 0.07
[2,] 0.0525 0.0525 0.0075 0.0075 0.03
[3,] 0.0175 0.0175 0.0025 0.0025 0.01
[4,] 0.0350 0.0350 0.0050 0.0050 0.02
[5,] 0.1225 0.1225 0.0175 0.0175 0.07

[[3]]
       [,1]   [,2]   [,3]   [,4] [,5]
[1,] 0.1225 0.1225 0.0175 0.0175 0.07
[2,] 0.1225 0.1225 0.0175 0.0175 0.07
[3,] 0.0350 0.0350 0.0050 0.0050 0.02
[4,] 0.0700 0.0700 0.0100 0.0100 0.04

> dfList
[[1]]
  X.alleles X.Freq
1         1   0.35
2         5   0.15
3         6   0.05
4         7   0.10
5         8   0.35

[[2]]
  Y.alleles Y.Freq
1         1   0.35
2         4   0.35
3         6   0.10
4         8   0.20

[[3]]
  Z.alleles Z.Freq
1         1   0.35
2         4   0.35
3         5   0.05
4         6   0.05


On 9/20/07, Luke Neraas <lukasneraas.r at gmail.com> wrote:
> #Hello,
>
>
> #I have three data frames, X,Y and Z with two columns each and different
> numbers of rows.
>
> # creation of data frame X
>
> X.alleles      <- c(1,5,6,7,8)
> X.Freq        <- c(0.35, 0.15, 0.05 , 0.10, 0.35)
> Loc1          <- cbind( X.alleles,X.Freq)
> X               <- data.frame(Loc1)
>
> #creation of data frame Y
>
> Y.alleles  <- c(1,4,6,8)
> Y.Freq     <- c(0.35, 0.35, 0.10, 0.20 )
> Loc2        <- cbind(Y.alleles, Y.Freq)
> Y             <- data.frame (Loc2)
>
> # creation of data frame Z
>
> Z.alleles  <- c(1,4,5,6,8)
> Z.Freq     <- c(0.35, 0.35, 0.05, 0.05, 0.20)
> Loc3       <- cbind(Z.alleles, Z.Freq)
> Z            <- data.frame (Loc3)
>
> X
> Y
> Z
>
> #  I want to create a pair wise multiplication for all of the second columns
> of my dataframe X,Y and Z
>
> # Here is a way to get two of the data frames to create a pairwise
> multiplication.
>
> X.Freq_times_Y.Freq<- matrix(Y[,2] %o% X[,2], ncol=1)
> X.Freq_times_Y.Freq
>
> # I would like to create a loop to calculate all possible pairwise
> multiplications for the
> # second columns of my X,Y, and Z data frames.
> # I will be conducting pair wise comparisons for up to 50 different data
> frames so I need the code to be
> # as flexible as possible
>
> Any help would be greatly appreciated.
>
> Thanks in advance
>
>
> Luke Neraas
> lukasneraas.r at gmail.com
>
> University of Alaska Fairbanks
> School of Fisheries and Ocean Sciences
> 11120 Glacier Highway
> UAF Fisheries Division
> Juneau, AK 99801
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list