[R] removing specific rows from array
Maas James Dr (MED)
J.Maas at uea.ac.uk
Sat Sep 18 13:53:02 CEST 2010
Thanks Michael,
Quite an effort! I failed to explain myself sufficiently clearly, what I need is for this if possible, to work with more than 4 treats, but still produce a matrix with all possible combinations of treatments (minus whichever treatment is in c1) in c2 and c3 but there would only be 1 comparison of 2 treatments ...
I managed to make two small changes to your code and it seems to work well. Now the vector "treats" can be any length. Thanks a bunch.
Jim
treats <- c("t0", "t1", "t2", "t3","t4")
n <- length(treats)
v <- 2 #number of treatments per meta-analysis comparison
n2 <- choose(n-1, v)
comb <- matrix("", nrow=n*n2, ncol=v+1)
k <- 1
for (i in 1:n) {
comb[k:(k+n2-1), ] <- cbind(treats[i], t(combn(treats[-i], v)))
k <- k + n2
}
===============================
Dr. Jim Maas
University of East Anglia
________________________________________
From: Michael Bedward [michael.bedward at gmail.com]
Sent: 18 September 2010 04:14
To: Maas James Dr (MED)
Cc: r-help at r-project.org
Subject: Re: [R] removing specific rows from array
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