-1

I have a requirement where in 70 odd data needs to be separated into 7 random data frame without any duplicate rows being selected, i.e. Replace = FALSE has been used still it picks duplicate rows even with sample_n() function results are the same.

Is it a bug known?

How will this be remedied as for future requirements this makes and arduous job to manually select.

df = name = c("arjun","Andrea", "Biswas","Ann","Biju", "Sheela","Deepti","Betty", "Hema", "Gowri"," Kunal", "Anamika","Ashik", "Hina","Kiran" )

gender = c("M","F", "M","M","F", "M","F","F","F", "F","M","F", "F","F","M")

etc like wise 5 with additional columns each group needs two females and rest males. but the basic splitting itself is having duplicates generated in group say arjun is in group 1 and 3 Andrea is in group 2 and 3 etc which should not happen.

code i tried

library(dplyr)
L4 = list()
dfc = list()
f = list()

numzone <- c(1:5)

for (i in numzone){

L4  <-  df[sample(nrow(df) ,size = 3 ,replace = FALSE),]




f<-paste("df", i, sep="")

dfc <- L4



if (i %in% c(1:5)) {

f <- dfc[]
}



print(f)

Also, additionally I need this separated rows be assigned to dynamic data frame, may be from a list defined.

1 Answers1

1

I don't think you found a bug, but it is hard to say without example data and the code you are using. I'd wager that you are running sample() (or whatever similar function) more than once, which even if you used replace=FALSE, could return the same value multiple times. In other words, replace=FALSE is only going to affect each call of sample().

If you'd like more specific advice, feel free to edit your question with an example dataset and the code you are using.

Here is a general approach to this problem. More information on sampling, here, and more information on splitting, here.

# values to be randomly sampled
pool <- 1:70

# dataframe to receive random samples
dat <- data.frame(matrix(ncol = 7, nrow = 0))

# take random samples, split into  7 groups
split(sample(x = pool, size = 70, replace = FALSE), f = ceiling(seq_along(pool)/10))

# rbind to other data
rbind(dat, split(sample(x = pool, size = 70, replace = FALSE), f = ceiling(seq_along(pool)/10)))
Skaqqs
  • 4,010
  • 1
  • 7
  • 21