Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to sample records from a database without repeating?

$
0
0

Good afternoon , my problem is as follow :

I have a database called friends :

friends <- data_frame(
  name = c("Nicolas", "Thierry", "Bernard", "Jerome", "peter", "yassine", "karim"),
  age = c(27, 26, 30, 31, 31, 38, 39),
  height = c(180, 178, 190, 185, 187, 160, 158),
  married = c("M", "M", "N", "N", "N", "M", "M")
)

i <- Intervals(
  matrix(
    c(0,5000,  
      0,5000,
      7000,10000,  
      7000,10000,
      7000,10000,
      10000,15000,  
      10000,15000
    ),
    byrow = TRUE,
    ncol = 2
  ),
  closed = c( TRUE, TRUE ),
  type = "R"
) 

I need to create a function that take this database as argument.

The function will sample a row ( for example the fourth row just one time , the function will not select this row for other executions ) , then it will operates some traitements.

sampling_fct<-function(data){

data[sample(nrow(data), 1), ]

# sample a given row only one time  

}

If we have 5 rows , selections should be like :

data[3]

data[2]

data[5]

data[4]

data1

where data = friends.

I shouldn't have duplicated results :

enter image description here

I wish my question is clear.

Thanks to you before !


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>