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
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. >
On Sep 18, 2010, at 11:25 AM, Santosh Srinivas wrote:> Strangely this is not working ... what am I doing wrong here? > >> tDate <- FnO_Data$Date[1] >> tDate > [1] 20090101 >> as.Date(c(tDate),format="%Y%m%d") > [1] NA?sasDate as.Date does not take numeric arguments. Try: > as.Date(as.character(tDate), format="%Y%M%d") [1] "2009-09-01" -- David Winsemius, MD West Hartford, CT
On Sat, Sep 18, 2010 at 11:25 AM, Santosh Srinivas <santosh.srinivas at gmail.com> wrote:> Strangely this is not working ... what am I doing wrong here? > >> tDate <- FnO_Data$Date[1] >> tDate > [1] 20090101 >> as.Date(c(tDate),format="%Y%m%d") > [1] NA >Do you have zoo loaded? If you do then a minimal reproducible example (see last line of every message to r-help) is:> library(zoo) > as.Date(20090101, format = "%Y%m%d")[1] NA Note that numeric arguments here are treated as the number of days since the Epoch and not as yyyymmdd. As others have pointed out this works:> as.Date(as.character(20090101), format = "%Y%m%d")[1] "2009-01-01" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Thanks. -----Original Message----- From: David Winsemius [mailto:dwinsemius at comcast.net] Sent: 18 September 2010 21:07 To: Santosh Srinivas Cc: r-help at r-project.org Subject: Re: [R] Date issues On Sep 18, 2010, at 11:25 AM, Santosh Srinivas wrote:> Strangely this is not working ... what am I doing wrong here? > >> tDate <- FnO_Data$Date[1] >> tDate > [1] 20090101 >> as.Date(c(tDate),format="%Y%m%d") > [1] NA?sasDate as.Date does not take numeric arguments. Try: > as.Date(as.character(tDate), format="%Y%M%d") [1] "2009-09-01" -- David Winsemius, MD West Hartford, CT
I tried this and it works too (For most part) .... strangely for certain dates (20090831) it is giving NA ...> FnO_Data$Date[m:l][1] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 [13] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 [25] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090831 20090831 [37] 20090831 20090831 20090831 20090831 20090831 20090831 20090831 20090831 20090831 20090831 20090831 20090831 [49] 20090831 20090831 20090831> as.Date(as.character(FnO_Data$Date[m:l]), format="%Y%M%d")[1] "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" [9] "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" [17] "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" [25] "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" [33] "2009-09-28" "2009-09-28" NA NA NA NA NA NA [41] NA NA NA NA NA NA NA NA [49] NA NA NA> sessionInfo()R version 2.11.1 (2010-05-31) i386-pc-mingw32 locale: [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C [5] LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] zoo_1.6-4 loaded via a namespace (and not attached): [1] grid_2.11.1 lattice_0.18-8 tools_2.11.1 -----Original Message----- From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Sent: 18 September 2010 21:27 To: Santosh Srinivas Cc: r-help at r-project.org Subject: Re: [R] Date issues On Sat, Sep 18, 2010 at 11:25 AM, Santosh Srinivas <santosh.srinivas at gmail.com> wrote:> Strangely this is not working ... what am I doing wrong here? > >> tDate <- FnO_Data$Date[1] >> tDate > [1] 20090101 >> as.Date(c(tDate),format="%Y%m%d") > [1] NA >Do you have zoo loaded? If you do then a minimal reproducible example (see last line of every message to r-help) is:> library(zoo) > as.Date(20090101, format = "%Y%m%d")[1] NA Note that numeric arguments here are treated as the number of days since the Epoch and not as yyyymmdd. As others have pointed out this works:> as.Date(as.character(20090101), format = "%Y%m%d")[1] "2009-01-01" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Make sure to look at as.character(FnO_Data$Date[m:l]) My $.02 /Henrik On Sat, Sep 18, 2010 at 10:31 PM, Santosh Srinivas <santosh.srinivas at gmail.com> wrote:> I tried this and it works too (For most part) .... strangely for certain > dates (20090831) it is giving NA ... > >> FnO_Data$Date[m:l] > ?[1] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 > 20090828 20090828 20090828 20090828 > [13] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 > 20090828 20090828 20090828 20090828 > [25] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 > 20090828 20090828 20090831 20090831 > [37] 20090831 20090831 20090831 20090831 20090831 20090831 20090831 20090831 > 20090831 20090831 20090831 20090831 > [49] 20090831 20090831 20090831 >> as.Date(as.character(FnO_Data$Date[m:l]), format="%Y%M%d") > ?[1] "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" > "2009-09-28" "2009-09-28" "2009-09-28" > ?[9] "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" > "2009-09-28" "2009-09-28" "2009-09-28" > [17] "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" > "2009-09-28" "2009-09-28" "2009-09-28" > [25] "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" "2009-09-28" > "2009-09-28" "2009-09-28" "2009-09-28" > [33] "2009-09-28" "2009-09-28" NA ? ? ? ? ? NA ? ? ? ? ? NA ? ? ? ? ? NA > NA ? ? ? ? ? NA > [41] NA ? ? ? ? ? NA ? ? ? ? ? NA ? ? ? ? ? NA ? ? ? ? ? NA ? ? ? ? ? NA > NA ? ? ? ? ? NA > [49] NA ? ? ? ? ? NA ? ? ? ? ? NA >> sessionInfo() > R version 2.11.1 (2010-05-31) > i386-pc-mingw32 > > locale: > [1] LC_COLLATE=English_United Kingdom.1252 ?LC_CTYPE=English_United > Kingdom.1252 > [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C > > [5] LC_TIME=English_United Kingdom.1252 > > attached base packages: > [1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base > > other attached packages: > [1] zoo_1.6-4 > > loaded via a namespace (and not attached): > [1] grid_2.11.1 ? ?lattice_0.18-8 tools_2.11.1 > > > -----Original Message----- > From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] > Sent: 18 September 2010 21:27 > To: Santosh Srinivas > Cc: r-help at r-project.org > Subject: Re: [R] Date issues > > On Sat, Sep 18, 2010 at 11:25 AM, Santosh Srinivas > <santosh.srinivas at gmail.com> wrote: >> Strangely this is not working ... what am I doing wrong here? >> >>> tDate <- FnO_Data$Date[1] >>> tDate >> [1] 20090101 >>> as.Date(c(tDate),format="%Y%m%d") >> [1] NA >> > > Do you have zoo loaded? ?If you do then a minimal reproducible example > (see last line of every message to r-help) is: > >> library(zoo) >> as.Date(20090101, format = "%Y%m%d") > [1] NA > > Note that numeric arguments here are treated as the number of days > since the Epoch and not as yyyymmdd. ?As others have pointed out this > works: > >> as.Date(as.character(20090101), format = "%Y%m%d") > [1] "2009-01-01" > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com > > ______________________________________________ > 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. >
On Sun, Sep 19, 2010 at 1:31 AM, Santosh Srinivas <santosh.srinivas at gmail.com> wrote:> I tried this and it works too (For most part) .... strangely for certain > dates (20090831) it is giving NA ... > >> FnO_Data$Date[m:l] > ?[1] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 > 20090828 20090828 20090828 20090828 > [13] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 > 20090828 20090828 20090828 20090828 > [25] 20090828 20090828 20090828 20090828 20090828 20090828 20090828 20090828 > 20090828 20090828 20090831 20090831 > [37] 20090831 20090831 20090831 20090831 20090831 20090831 20090831 20090831 > 20090831 20090831 20090831 20090831 > [49] 20090831 20090831 20090831 >> as.Date(as.character(FnO_Data$Date[m:l]), format="%Y%M%d")Its %m, not %M. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com