#examples for PSY533 Lab 10//8/08 Ted Walls URI #setting up a vector based on a sequence x = seq(0, 300, by=10) x #some vector arithmetic y = x/2 y #a correlation cor(x,y) plot(x,y) #creating the vector length (n) length(x) #generating a normal distribution p = rnorm(31, 10, 2) #n, mu, sigma p #showing no association of x and q cor(x,p) plot(x,p) # If statements in R--no 'then' <- means = x <- 4 if (x > 3) y <- 3 * x else y <- 4 * x y #missing values in R--shows missing values as NA x <- NULL #vector with no elements x[seq(2,20,2)] <- seq(2,10,2) x #use a command to detect missing values is.na(x) #or the opposite 'not' is.na --R uses ! !is.na(x) x[which(is.na(x))] #show the numbers in x that are missing x #show the mean of just the values that are not missing mean(x, na.rm=TRUE) #the rm function removes the missing values hist(x) x #drop some information from x with if statement if (x < 8) y = x y