How to create sample of rows using ID column in R?

R ProgrammingServer Side ProgrammingProgramming

To create sample of rows using ID column, we can use sample function. We would need to apply the sample function on ID column and take the subset of rows with the help of single square brackets.

For example, if we have a data frame called df that contains an ID column say ID then we can sample 4 rows of df using ID column as follows −

df[sample(df$ID,4),]

Example 1

Following snippet creates a sample data frame −

Emp_ID<-1:20
Salary<-sample(20000:50000,20)
df1<-data.frame(Emp_ID,Salary)
df1

Output

The following dataframe is created −

 Emp_ID Salary
1   1  34189
2   2  30385
3   3  32484
4   4  42169
5   5  34809
6   6  30039
7   7  40836
8   8  27534
9   9  39775
10 10  25075
11 11  37880
12 12  43213
13 13  44057
14 14  33249
15 15  49177
16 16  34334
17 17  32384
18 18  33653
19 19  21515
20 20  29009

To sample five rows of df1 based on Emp_ID column, add the following code to the above snippet −

Emp_ID<-1:20
Salary<-sample(20000:50000,20)
df1<-data.frame(Emp_ID,Salary)
df1[sample(df1$Emp_ID,5),]

Output

If you execute all the above given snippets as a single program, it generates the following output −

 Emp_ID Salary
9   9  39775
16 16  34334
19 19  21515
20 20  29009
11 11  37880

Example 2

Following snippet creates a sample data frame −

Student_ID<-sample(1:10,20,replace=TRUE)
Score<-sample(1:10,20,replace=TRUE)
df2<-data.frame(Student_ID,Score)
df2

Output

The following dataframe is created −

Student_ID Score
1   3   8
2  10   1
3   5   6
4   1  10
5   1   1
6   4   2
7   2   3
8   6  10
9   9   3
10  1   4
11  7   3
12  4   1
13  4   3
14  1   8
15  2   8
16  7   8
17  7   4
18  4   4
19  1   3
20 10   1

To sample five unique rows of df2 based on Student_ID column, add the following code to the above snippet −

Student_ID<-sample(1:10,20,replace=TRUE)
Score<-sample(1:10,20,replace=TRUE)
df2<-data.frame(Student_ID,Score)
df2[sample(unique(df2$Student_ID),5),]

Output

If you execute all the above given snippets as a single program, it generates the following output −

Student_ID Score
7   2      3
3   5      6
9   9      3
4   1     10
2  10      1
raja
Updated on 05-Nov-2021 07:52:14

Advertisements