How to use Sample_n() function In R Language

Sample_n() function is used to select n random rows from a dataframe in R. This is one of the widely used functions of the R programming language as this function is used to test the various user build models for prediction and for accuracy purposes.

Syntax: sample_n(tbl, size, replace, fac, …)

Parameters:

  • tbl: a Momocs object (Coo, Coe)
  • size: numeric how many shapes should we sample
  • replace: logical whether the sample should be done with or without replacement
  • fac: a column name if a $fac is defined; size is then applied within levels of this factor
  • …: additional arguments to dplyr::sample_n and to maintain generic compatibility

Return:

This function will be returning the random samples from the provided data of size n.

Example: R program to generate random sample using sample_n()

R




library(dplyr)
  
gfg_data <- iris
sample_n(gfg_data,10)


Output:

Select Random Samples in R using Dplyr

In this article, we will be looking at different methods for selecting random samples from the Dplyr package of the R programming language.

To install and import the Dplyr package in the R programming language, the user needs to follow the syntax:

Syntax:

install.packages(“dplyr”)

library(dplyr)

Similar Reads

Method 1: Using Sample_n() function

Sample_n() function is used to select n random rows from a dataframe in R. This is one of the widely used functions of the R programming language as this function is used to test the various user build models for prediction and for accuracy purposes....

Method 2: Using Sample_frac() function

...