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, 2022 in Data Science by Avinash
• 1,260 points
484 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)

Unleash the power of data with our comprehensive Data Science Training.

answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Science

0 votes
1 answer

sample function in R

It seems like you are experiencing an ...READ MORE

answered Sep 8, 2023 in Data Science by anonymous
• 1,380 points
614 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, 2022 in Data Science by avinash
• 1,840 points
562 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, 2022 in Data Science by Sohail
• 3,040 points
462 views
0 votes
1 answer

Unexpected behavior for setdiff() function in R

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

answered Jun 24, 2022 in Data Science by Sohail
• 3,040 points
954 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,370 points
565 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,370 points
861 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,370 points
5,108 views
0 votes
0 answers

100 samples of 20 from the dataset and drawing regression lines along with population regression line

I have a datasetwith two variables hours ...READ MORE

Apr 11, 2022 in Machine Learning by Dev
• 6,000 points
754 views
0 votes
1 answer

Creating new Functions with Linear Regression in R :

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

answered Jun 1, 2022 in Data Science by Sohail
• 3,040 points
565 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, 2022 in Data Science by Sohail
• 3,040 points
645 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP