# Intro to R & S+ # R. Chris Fraley | fraley@uic.edu # Psych 543 # University of Illinois at Chicago # Last updated: Oct 10, 2002 #----- CREATING VARIABLES and VECTORS ---------- # To assign a numerical value to a symbol, use # the following: <- # If we were to type x<-1, we would be telling the program # to let x equal 1. x<-1 print(x) x<-10 print(x) # You can also assign multiple values to a variable, # thereby creating a vector. There are several ways to # do this. Here are a few: # The c() command allows you to concatenate # the values within (). x<-c(1,2,3,4,5) print(x) # The use of a colon allows you to create # an ordered sequence of values between 1 and 10 # in this example. x<-1:10 print(x) # The seq(b,e,s) command is quite useful. # It creates an ordered sequence of values # starting at b, ending at e, and # advancing in increments of s. x<-seq(1,10,1) print(x) x<-seq(-4,4,.1) print(x) # ---------- BASIC MATHEMATICAL OPERATIONS-------------- 1+2 # addition 5-6 # subtraction 2*5 # multiplication 5/2 # division 5^2 # raise 5 to the second power (square) 5^3 # raise 5 to the third power sqrt(25) # square root of 25 (2*3)/(5*2) # use parentheses as needed # Note: These operations can be performed with real values (as # shown above) or with variables (as shown below). x<-2 y<-3 x+y x-y (x^y)/(y^x) #-------------- BASIC PROGRAMMING AND LOGICAL OPERATIONS ---------------- # Many simulations that you may want to conduct will require # the use of loops--subprograms that perform some kind of # operation multiple times. There are several ways to # create loops or subroutines in R/S+. One of the most common # is called a for-next loop. The way this loop works is best # understood via example: x<-1 for(i in 1:10){ x<-x+1 print(x) } # This loop is designed to take the value of # x and add it to itself 10 times. # The 1:10 part of the command tells the program # to loop over the commands in brackets 10 times. # Each time the loop begins, the value of i, which # acts as a counter or a timer, is incremented by one # unit. Thus, the first time through the loop, i equals # 1. The second time through, i equals 2. The tenth time # through, i equals 10. # In this program, x is set to be equal to 1 before the # loop begins. In the first round, x is assigned to # equal x + 1. Since x = 1 in the first round, x is # now assigned to equal 1 + 1 or 2. Now, x equals 2 # as the second round begins. x is reassigned to equal # x + 1 or (2+1) = 3. Thus, after the second round through # the loop, x equals 3. After 10 rounds through the loop, # x will equal 11. # The print command tells R/S+ to print the current value of # x. Since this command is embedded within the loop, # the program will print x each time it passes through. # Here is another loop example: y<-1:10 for(i in 2:10){ y[i]<-y[i-1]*2 } print(y) # In this example, we begin by creating a vector called y. # Initially, y is simply the numbers 1 through 10. # Then, the loop begins. We begin the loop at 2 instead of 1; # thus, the operations contained in the loop will only # be performed 9 times. # The operation involves taking the (i-1) element of y, multiplying # it by 2, and assigning the result to the ith element of y. # Thus, the first time through the loop, i = 2, so we take the # first (i - 1) element of y, which is one, multiply it by two, # and save product (2) as the ith (second) element of y. The next time # through the loop, i = 3, so we take the second (i - 1) element # of y (which is 2), multiply it by 2, and save the product (4) as the # ith element (3) of y. When the loop is finished, the print() # command shows you all the elements of y. # You will also need to use "logicals" from time to time. # Consider the following example: x<-10 y<-20 if(x > y){ print("X is bigger than Y") } if(x < y){ print("Y is bigger than X") } # In this example, we begin by letting x equal 10 # and y equal 20. # To determine whether x is bigger than y, we can use # the if(){} operation. In this command, place the # conditional (e.g., x > y) in the parentheses, and # place the commands that you want the computer to carry out # if the conditional is true in the brackets. # In this program, if x is greater than y, the program # prints the statement "X is bigger than Y." If the # condition is false (i.e., if x < or equal to y), # the program performs the operation within the brackets # of the next statement. # Here is another example that combines if routines and # for-next loops: y<-1:10 for(i in 2:10){ y[i]<-y[i-1]*2 if(y[i]>50){ print("y is greater than 50") } if(y[i]<50){ print("y is smaller than 50") } } # In this example, the program tells us whether the # current value of y is smaller than or greater than 50. # Here is a partial list of logical operations # that can be used in S+/R: # S+/R will respond with a T (true) or F (false) # for logical operations. # We'll begin by letting x = 10 and y = 20: x<-10; y<-20 x>y # Is x greater than y? x=x # Is x greater than or equal to y? x<=y # Is x less than or equal to y? x!=y # Does x not equal y? x==y # Does x equal y? #---------------------- BASIC STATISTICAL FUNCTIONS---------- # S+/R allows you to perform a large number of statistical # operations. Let's begin by drawing 10 cases at # random from a normal distribution with # a population mean of 0 and a population # sd of 1: x<-rnorm(n=10,mean=0,sd=1) print(x) # The rnorm command allows you to take a sample of size n # from a distribution of normally distributed scores # with specificed mean and sd. We assigned these # 10 scores to the variable x. # We can draw another sample of 10 scores from a normal # distribution with a mean of 20, and assign these scores # to the variable y: y<-rnorm(n=10,mean=20,sd=1) print(y) # Now, let's calculate some simple statistics on these # two variables. Try the following commands: mean(y) # find the mean of y var(y) # find the variance of y sqrt(var(x)) # find the SD of x (the square root of the variance) var(x,y) # find the covariance between x and y cor(x,y) # find the correlation between x and y # we'll elaborate on basic statistical operations in the next tutorial