Appendix BCommonly Used R Commands

B.1 How to Run R Commands

There are several ways to run R commands:

  • Type a command in the R Studio console window (after the prompt symbol >), and press Enter.
  • Run codes in an R script file.R in the R Studio source window (where you can read, create, and edit R scripts) once the file is open. This is recommended for readers to run the R scripts in each chapter. By clicking the Run button (located above the R script file, at the top right of the editing window), R will execute the current line (where the cursor is). You can also select (highlight) multiple lines in an R script file and click Run to execute them all at once.

B.2 General Commands

Note that the > symbol at the beginning of each R command in the R Studio console window is a prompt generated by R.

  • Get R help files for a function

    Type “?” followed by a command name, for example:

     > ?library > ?matrix 
  • Set the working directory

    By setting the working directory in R using command setwd, files can be read from or written to this default location, for example

     > setwd("C:/Bayesian") 
  • Check the current working directory
     > getwd() 
  • Install R packages
     > install.packages("mcmcplots") 
  • Load packages
     > library(rjags) 

B.3 Generate Data

  • Assign a value to a variable

    There are two ways to assign a value to a variable in R:

     > x <- 3 

    or

     > x = 3 

    Both of these commands assign the value 3 to the variable x. When you type x, its value will be shown.

     > x [1] 3 
  • Concatenate (combine) function
     > c(1,3,5) ...

Get Practical Applications of Bayesian Reliability now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.