Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a for loop to choose randomly years without replacement in R

I have a string g <- c(1979:2020) and I would like to create 8 random groups of 4 years each and two with 5 years without replacement. What I do is:

group1 <- sample(g, size = 4)
g1 <- g[!(g %in% group1)]
group2 <- sample(g1, size = 4)
g2 <- g[!(g %in% group2)] etc

Is there a smarter way to do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can create the entire vector with sample and size = length(g), and then create groups by splitting it every four elements for the first 8 groups (with gl) and then 5 times for the two remaining group.

sample(g, size = length(g)) |>
  split(c(gl(8, 4), gl(2, 5, labels = 9:10)))

Or, in a for loop, you can do:

g <- c(1979:2020)
l <- vector(10, mode = "list")
for (i in seq(10)){
  
  if(i %in% seq(8)){
    l[[i]] <- sample(g, size = 4)
    g <- g[!(g %in% unlist(l))]
  }
  
  else if (i %in% 9:10){
    l[[i]] <- sample(g, size = 5)
    g <- g[!(g %in% unlist(l))]
  }
}

output:

#> l
[[1]]
[1] 2010 1980 1983 2014

[[2]]
[1] 2019 2004 1990 1997

[[3]]
[1] 1981 1992 1979 2018

[[4]]
[1] 1986 2005 2008 2003

[[5]]
[1] 1987 1984 1996 1985

[[6]]
[1] 1982 1993 2020 2006

[[7]]
[1] 1995 1994 2017 1998

[[8]]
[1] 1989 1999 2012 1991

[[9]]
[1] 2013 2009 2016 2002 2000

[[10]]
[1] 2001 2015 1988 2007 2011
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading