Problem with sample function in R

0 votes
I'm trying to use R's sample function to split a sequence of integers into numerous equal portions for later usage, but even when I specify that I don't want replacement values, I'm still getting repeated digits.

set.seed seq(1,21,1) trials (5)
p1.trials - sample(trials, 7, replace=F) p1.trials - sample(trials, 7, replace=F) p1.trials - sample(trials, 7, replace=F) p
As a result, you'll get the following vector: 5, 14, 18, 6, 2, 12, and 8 are all numbers that can be used to make a number.

trials - tests

[-p1.trials]
sample(trials, 7, replace=F) p2.trials
p2.trials
As a result, you'll get the following vector: 19, 20, 3, 7, 9, 4, 16,

p3.trials - trials[-p2] p3.trials - trials[-p2] p3.trials .trials]
p3.trials
As a result, you'll get the following vector: 1–3, 9, 10–13, 16, 17–19, 20, 21

Can someone explain why (a) I'm receiving repeated values (e.g., "3" appears in both p2.trials and p3.trials) and (b) I'm getting repeated values (e.g., "3" appears in both p2.trials and p3.trials)?
Jun 19 in Data Science by Avinash
• 1,260 points
18 views

1 answer to this question.

0 votes
The first time works, but after that, the value and index of each trial's member are different. The issue is caused by using -; instead, use setdiff:

trials in order (1,21,1)

set.seed(5)

sample(p1.trials, 7, replace=F)

p1.trials

setdiff - trials (trials,p1.trials)
answered Jun 24 by Sohail
• 2,960 points

Related Questions In Data Science

0 votes
0 answers

sample function in R

I've only recently started using RStudio to ...READ MORE

Jul 20 in Data Science by avinash
• 1,840 points
7 views
0 votes
0 answers

Introduction to Statistical Learning with Applications in R Figure Codes

I recently bought the following book: An Introduction ...READ MORE

Jun 1 in Data Science by avinash
• 1,840 points
33 views
0 votes
1 answer

How to implement Knn-algorithm without using k-nn function in r?

I created an example that demonstrates the ...READ MORE

answered Jun 24 in Data Science by Sohail
• 2,960 points
14 views
0 votes
1 answer

Unexpected behavior for setdiff() function in R

Asymmetric difference is provided by 18 setdiff. ...READ MORE

answered Jun 24 in Data Science by Sohail
• 2,960 points
23 views
0 votes
1 answer

R: Sample from a neighborhood according to scores

I would suggest you to use the truncated ...READ MORE

answered May 29, 2018 in Data Analytics by Sahiti
• 6,380 points
184 views
0 votes
1 answer

How to sample random rows in dataframe?

Create data frame and then implement as ...READ MORE

answered Jul 3, 2018 in Data Analytics by Sahiti
• 6,380 points
391 views
0 votes
1 answer

How to sample n random rows per group in a dataframe?

You can assign a random ID to ...READ MORE

answered Jul 3, 2018 in Data Analytics by Sahiti
• 6,380 points
3,802 views
0 votes
0 answers
0 votes
1 answer

Creating new Functions with Linear Regression in R :

When we want to create a model ...READ MORE

answered Jun 1 in Data Science by Sohail
• 2,960 points
30 views
0 votes
1 answer

rnorm function in R - Usage

y = rnorm(12, rep(c(1,2,1), each=4, 0.2)) I can ...READ MORE

answered Jun 24 in Data Science by Sohail
• 2,960 points
27 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP