# This is an R-script to familiarize you with R, # and show you some of the plotting commands. # Lines starting with `#' contain comments and explanations. # to get help on any command, type `?' before the command ####################### # Some general things # ####################### # You can use R as a calculator: 5+3 # Create a vector, using the command 'c()' (for concatenate) x <- c(1,2,3,4) x # Sum all elements of x: sum(x) # Square all elements of x: x^2 # Get the third element of x: x[3] # Add an extra element: x <- c(x,10) x # Create a vector using the command 'seq()' (for sequence) x <- seq(from=1, to=10, by=.1) x # Obtain the length of x: length(x) # Create a vector using the command 'rep()' (for repeat) x <- rep(0,times=10) x #################### # load a data set: # #################### # load the car library which contains all data sets: # (Before you can do this, you need to install the package 'car', # using the menu: Packages -> Install Package(s) # You only need to do this once on each computer you work on.) library(car) # Get a description of the data set `Duncan': ?Duncan # Load the data set: data(Duncan) # Look at the data: Duncan # Get summary of the data: summary(Duncan) # If there are missing values, then these are denoted by 'NA' ##################################### # Access various parts of the data: # ##################################### # The data are stored in a matrix. # We can access the entries in the data set. For example, # for the entry in the second row and the third column: Duncan[2,3] # Obtain the first column: Duncan[,1] # Obtain the first row: Duncan[1,] # We can exclude a column or row by using the minus sign: Duncan[,-1] # We can also access the columns by their names, using a $ sign to # separate the name of the data set and the name of the column: Duncan$income # Note that the following doesn't work: income # But if we first attach the data, it does work: attach(Duncan) income # This can save you some typing. # To detach the dataset, use the command `detach(Duncan)' or `detach()'