[R] removing specific rows from array

Michael Bedward michael.bedward at gmail.com
Sat Sep 18 05:14:33 CEST 2010


Here's one way...

treats <- c("t0", "t1", "t2", "t3")
n <- length(treats)
n2 <- choose(n-1, n-2)
comb <- matrix("", nrow=n*n2, ncol=n-1)
k <- 1
for (i in 1:n) {
  comb[k:(k+n2-1), ] <- cbind(treats[i], t(combn(treats[-i], n-2)))
  k <- k + n2
}

It doesn't get any marks for conciseness but it should work :)

Michael


On 18 September 2010 00:51, Maas James Dr (MED) <J.Maas at uea.ac.uk> wrote:
> I'm attempting to create an array of treatment comparisons for modelling data generation.  This involves comparison of one treatment (c2) with another (c3), relative to a common comparator (c1).
>
> Attached code gives me the correct array but need to remove duplicates.  Duplicates relate only to c2 and c3
> such that I need to remove
>
> r3 because c2 and c3 are same as r1 with c2 and c3 swapped
> r5 because c2 and c3 are same as r2 with c2 and c3 swapped
> r6 because c2 and c3 are same as r4 with c2 and c3 swapped
> r9 because c2 and c3 are same as r7 with c2 and c3 swapped
> r11 because c2 and c3 are same as r8 with c2 and c3 swapped
> .
> .
> .
>
> Any suggestions?
>
> Thanks
>
> Jim
>
>
>
>> treats <- c("t0","t1","t2","t3")
>> (combs1 <- permutations(length(treats),3,treats))
>      [,1] [,2] [,3]
>  [1,] "t0" "t1" "t2"
>  [2,] "t0" "t1" "t3"
>  [3,] "t0" "t2" "t1"
>  [4,] "t0" "t2" "t3"
>  [5,] "t0" "t3" "t1"
>  [6,] "t0" "t3" "t2"
>  [7,] "t1" "t0" "t2"
>  [8,] "t1" "t0" "t3"
>  [9,] "t1" "t2" "t0"
> [10,] "t1" "t2" "t3"
> [11,] "t1" "t3" "t0"
> [12,] "t1" "t3" "t2"
> [13,] "t2" "t0" "t1"
> [14,] "t2" "t0" "t3"
> [15,] "t2" "t1" "t0"
> [16,] "t2" "t1" "t3"
> [17,] "t2" "t3" "t0"
> [18,] "t2" "t3" "t1"
> [19,] "t3" "t0" "t1"
> [20,] "t3" "t0" "t2"
> [21,] "t3" "t1" "t0"
> [22,] "t3" "t1" "t2"
> [23,] "t3" "t2" "t0"
> [24,] "t3" "t2" "t1"
>>
>
>
> ===============================
> Dr. Jim Maas
> University of East Anglia
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list