Learn R Programming

SciencesPo (version 0.11.21)

sample.df: Extract a simple random sample from a data frame

Description

Extract a simple random sample from a data.frame.

Usage

sample.df(df, n, replace = FALSE, vars = NULL)

Arguments

df
The data frame to be sampled from.
n
Number of observations (rows) to sample.
replace
A logical indicating whether sampling with replacement or not (default).
vars
A list of variables to include in the sampled data, the default is to include all

Value

  • Returns the same object as df. Optionally, only includes the columns listed in vars.

Examples

Run this code
data(tobaccovote)

# sample all variables without replacement
( ans1 <- sample.df(tobaccovote,10) )

# sample two variables without replacement
( ans2 <- sample.df(tobaccovote,25,vars=c("votedpro","party")) )

# sample two variables with replacement
( ans3 <- sample.df(tobaccovote,25,replace=TRUE,vars=c("votedpro","party")) )

Run the code above in your browser using DataLab