John Sorkin
2015-Apr-19 13:44 UTC
[R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n
Windows 7 64-bit R 3.1.3 RStudio 0.98.1103 I am trying to generate a list of length 4n which consists of the integers 1 to n repeated in groups of four, i.e. 1,1,1,1, 2,2,2,2, 3,3,3,3, . . . . , n,n,n,n (The spaces in the list are added only for clarity.) I can generate the list as follows, but the code must be modified for any value n, and the code is UGLY! c(rep(1,4), rep(2,4), rep(3,4), . . . ,c(n,4)) Can anyone help me? Thank you, John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Call Send SMS Add to Skype You'll need Skype CreditFree via Skype John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Confidentiality Statement: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
Ben Tupper
2015-Apr-19 22:22 UTC
[R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n
On Apr 19, 2015, at 9:44 AM, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:> Windows 7 64-bit > R 3.1.3 > RStudio 0.98.1103 > > > I am trying to generate a list of length 4n which consists of the integers 1 to n repeated in groups of four, i.e. > > 1,1,1,1, 2,2,2,2, 3,3,3,3, . . . . , n,n,n,n >Hi, Like this?> x <- 1:4 > xx <- rep(x, each = 4) > xx[1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 Cheers, Ben> (The spaces in the list are added only for clarity.) > > I can generate the list as follows, but the code must be modified for any value n, and the code is UGLY! > > c(rep(1,4), rep(2,4), rep(3,4), . . . ,c(n,4)) > > Can anyone help me? > > Thank you, > John > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Call > Send SMS > Add to Skype > You'll need Skype CreditFree via Skype > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:25}}
Jeff Newmiller
2015-Apr-19 23:14 UTC
[R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n
You are not generating lists, you are generating vectors. Try rep( seq.int( n ), each= 4 ) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On April 19, 2015 6:44:56 AM PDT, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:>Windows 7 64-bit >R 3.1.3 >RStudio 0.98.1103 > > >I am trying to generate a list of length 4n which consists of the >integers 1 to n repeated in groups of four, i.e. > >1,1,1,1, 2,2,2,2, 3,3,3,3, . . . . , n,n,n,n > >(The spaces in the list are added only for clarity.) > >I can generate the list as follows, but the code must be modified for >any value n, and the code is UGLY! > >c(rep(1,4), rep(2,4), rep(3,4), . . . ,c(n,4)) > >Can anyone help me? > >Thank you, >John > >John David Sorkin M.D., Ph.D. >Professor of Medicine >Chief, Biostatistics and Informatics >University of Maryland School of Medicine Division of Gerontology and >Geriatric Medicine >Baltimore VA Medical Center >10 North Greene Street >GRECC (BT/18/GR) >Baltimore, MD 21201-1524 >(Phone) 410-605-7119410-605-7119 >(Fax) 410-605-7913 (Please call phone number above prior to faxing) > > >Call >Send SMS >Add to Skype >You'll need Skype CreditFree via Skype > >John David Sorkin M.D., Ph.D. >Professor of Medicine >Chief, Biostatistics and Informatics >University of Maryland School of Medicine Division of Gerontology and >Geriatric Medicine >Baltimore VA Medical Center >10 North Greene Street >GRECC (BT/18/GR) >Baltimore, MD 21201-1524 >(Phone) 410-605-7119410-605-7119 >(Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > > >John David Sorkin M.D., Ph.D. >Professor of Medicine >Chief, Biostatistics and Informatics >University of Maryland School of Medicine Division of Gerontology and >Geriatric Medicine >Baltimore VA Medical Center >10 North Greene Street >GRECC (BT/18/GR) >Baltimore, MD 21201-1524 >(Phone) 410-605-7119 >(Fax) 410-605-7913 (Please call phone number above prior to faxing) > > >Confidentiality Statement: >This email message, including any attachments, is for t...{{dropped:12}}
William Dunlap
2015-Apr-19 23:26 UTC
[R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n
Look more at help(rep) and help(seq): > n <- 7 > rep(seq_len(n), each=4) [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 Bill Dunlap TIBCO Software wdunlap tibco.com On Sun, Apr 19, 2015 at 6:44 AM, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:> Windows 7 64-bit > R 3.1.3 > RStudio 0.98.1103 > > > I am trying to generate a list of length 4n which consists of the > integers 1 to n repeated in groups of four, i.e. > > 1,1,1,1, 2,2,2,2, 3,3,3,3, . . . . , n,n,n,n > > (The spaces in the list are added only for clarity.) > > I can generate the list as follows, but the code must be modified for any > value n, and the code is UGLY! > > c(rep(1,4), rep(2,4), rep(3,4), . . . ,c(n,4)) > > Can anyone help me? > > Thank you, > John > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Call > Send SMS > Add to Skype > You'll need Skype CreditFree via Skype > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Confidentiality Statement: > This email message, including any attachments, is for the sole use of the > intended recipient(s) and may contain confidential and privileged > information. Any unauthorized use, disclosure or distribution is > prohibited. If you are not the intended recipient, please contact the > sender by reply email and destroy all copies of the original message. > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
Rmh
2015-Apr-20 00:50 UTC
[R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n
rep(1:n, each=4) Sent from my iPhone> On Apr 19, 2015, at 09:44, John Sorkin <JSorkin at grecc.umaryland.edu> wrote: > > Windows 7 64-bit > R 3.1.3 > RStudio 0.98.1103 > > > I am trying to generate a list of length 4n which consists of the integers 1 to n repeated in groups of four, i.e. > > 1,1,1,1, 2,2,2,2, 3,3,3,3, . . . . , n,n,n,n > > (The spaces in the list are added only for clarity.) > > I can generate the list as follows, but the code must be modified for any value n, and the code is UGLY! > > c(rep(1,4), rep(2,4), rep(3,4), . . . ,c(n,4)) > > Can anyone help me? > > Thank you, > John > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Call > Send SMS > Add to Skype > You'll need Skype CreditFree via Skype > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:12}}
Boris Steipe
2015-Apr-20 01:05 UTC
[R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n
That would be the "each" argument to rep()... n <- 5 rep(1:n, each=4) [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 Cheers, B. On Apr 19, 2015, at 9:44 AM, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:> Windows 7 64-bit > R 3.1.3 > RStudio 0.98.1103 > > > I am trying to generate a list of length 4n which consists of the integers 1 to n repeated in groups of four, i.e. > > 1,1,1,1, 2,2,2,2, 3,3,3,3, . . . . , n,n,n,n > > (The spaces in the list are added only for clarity.) > > I can generate the list as follows, but the code must be modified for any value n, and the code is UGLY! > > c(rep(1,4), rep(2,4), rep(3,4), . . . ,c(n,4)) > > Can anyone help me? > > Thank you, > John > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Call > Send SMS > Add to Skype > You'll need Skype CreditFree via Skype > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:12}}
SOUVIK BANDYOPADHYAY
2015-Apr-20 04:58 UTC
[R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n
Hi, You can use the apply group of functions n<-5 k<-4 unlist(lapply(1:n,rep, each=k)) # For vector output: sapply(1:n,rep, each=k) # For a matrix output Hope this helps Souvik On Sun, Apr 19, 2015 at 7:14 PM, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:> Windows 7 64-bit > R 3.1.3 > RStudio 0.98.1103 > > > I am trying to generate a list of length 4n which consists of the > integers 1 to n repeated in groups of four, i.e. > > 1,1,1,1, 2,2,2,2, 3,3,3,3, . . . . , n,n,n,n > > (The spaces in the list are added only for clarity.) > > I can generate the list as follows, but the code must be modified for any > value n, and the code is UGLY! > > c(rep(1,4), rep(2,4), rep(3,4), . . . ,c(n,4)) > > Can anyone help me? > > Thank you, > John > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Call > Send SMS > Add to Skype > You'll need Skype CreditFree via Skype > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:24}}
Sven E. Templer
2015-Apr-20 06:55 UTC
[R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n
In '?rep' find out about the 'each' argument. Also there is the function 'gl' which creates a factor and offers a shorter syntax for your problem. If n equals 5 use one of: rep(seq(5), each = 4) gl(5,4) On 19 April 2015 at 15:44, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:> Windows 7 64-bit > R 3.1.3 > RStudio 0.98.1103 > > > I am trying to generate a list of length 4n which consists of the > integers 1 to n repeated in groups of four, i.e. > > 1,1,1,1, 2,2,2,2, 3,3,3,3, . . . . , n,n,n,n > > (The spaces in the list are added only for clarity.) > > I can generate the list as follows, but the code must be modified for any > value n, and the code is UGLY! > > c(rep(1,4), rep(2,4), rep(3,4), . . . ,c(n,4)) > > Can anyone help me? > > Thank you, > John > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Call > Send SMS > Add to Skype > You'll need Skype CreditFree via Skype > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:16}}