12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

If you want to know what a minimal reproducible example looks like, take alook at the examples in the R Help files. In general, all the code given in the RHelp files fulfills the requirements of a minimal reproducible example.Creating sample data with random valuesIn most cases, you can use random data to illustrate a problem. R has someuseful built-in functions to generate random numbers and other random data.For example, to make a vector of random numbers, use rnorm() for the normaldistribution or runif() for a uniform distribution. To make a random vectorwith five elements, try the following:> set.seed(1)> x x[1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078You can use the set.seed() function to specify a starting seed value forgenerating random numbers. By setting a seed value, you guarantee that therandom numbers are the same each time you run the code. This sounds a bitpointless, doesn’t it? It may be pointless in production code, but it’s essentialfor a reproducible example. By setting a seed, you guarantee that your codewill produce the same results as another person running your code.If you want to generate random values of a predetermined set, use thesample() function. This function is a bit like dealing from a deck of playingcards. In a card game, you have 52 cards and you know exactly which cardsare in the deck. But each deal will be different. You can simulate dealing ahand of seven cards using the following code:> cards suits deck set.seed(123)> sample(deck, 7)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!