Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

swap

Swap values among columns of a data frame


Description

Swap values among columns of a data frame

Usage

swap(data, which)

Arguments

data

a data frame

which

a formula or an integer or character vector specifying columns in data

Details

swap is not a particularly speedy function. It is intended primarily as an aid for teaching randomization for paired designs. Used this way, the number of randomizations should be kept modest (approximately 1000) unless you are very patient.

Examples

if (require(tidyr)) {
  Sleep2 <- sleep %>% spread( key=group, val=extra )
  names(Sleep2) <- c("subject", "drug1", "drug2")
  swap(Sleep2, drug1 ~ drug2)
  mean( ~(drug1 - drug2), data=Sleep2)
  do(3) * mean( ~(drug1 - drug2), data=Sleep2 %>% swap(drug1 ~ drug2) ) 
}

mosaic

Project MOSAIC Statistics and Mathematics Teaching Utilities

v1.8.3
GPL (>= 2)
Authors
Randall Pruim [aut, cre], Daniel T. Kaplan [aut], Nicholas J. Horton [aut]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.