Before starting this session, you will want to restore the work you did in the previous session that you saved to your flash drive. You do it as follows:
> setwd("F:\\psych018"") > load(".Rdata") > loadhistory()
In the first line, you should put the name of the directory you used to save your work at the end of the last session (we used F:\\psych018; your mileage may vary). The second line loads in all the data you saved there, and the third line loads the history of all the commands you typed during your previous session.
To select an item or items from a vector, you put the item number or numbers inside of square brackets. So, if you wanted to see the first item in a vector named distance, you would type distance[1]. If you wanted to see items 3 and 5 in that vector, you would type this: distance[c(3, 5)] — notice that, because you want more than one item, you must combine the item numbers before putting them into the square brackets.
Create the following vector of items, please, and see what happens when you type the commands that follow. (If you don’t want to type all the values, just copy and paste the input command with the c(...) into your R window. I strongly recommend that you type the other shorter commands by hand rather than just copying and pasting. Also, feel free to experiment and try other variations. The worst you can get is an error message that tells you “you can’t do that.”
> score <- c(11,22,33,44,55,66,77,88,99) > score [1] 11 22 33 44 55 66 77 88 99 > score[1] [1] 11 > score[c(3,5,7)] [1] 33 55 77 > score[c(2,3,4)] # a set of consecutive entries [1] 22 33 44 > score[2:4] # a shorter way to do the same thing [1] 22 33 44 > length(score) # how many items are in this vector? [1] 9 > score[-2] # everything EXCEPT item 2 [1] 11 33 44 55 66 77 88 99 > score[-(2:4)] # everything EXCEPT items 2 through 4 [1] 11 55 66 77 88 99 > score[10] # beyond the end of the vector is a "missing value" [1] NA
While it’s possible to get individual entries from a vector, you will usually want to get a set of items from the vector, and you won’t know which particular ones they are. For example, you may want to extract all the numbers in a vector whose value is greater than 50. Reset the score vector by typing this set of scores on a personality inventory.
> score <- c(47, 82, 65, 81, 39, 72, 89, 49, 52, 76) > score [1] 47 82 65 81 39 72 89 49 52 76 > score > 50 [1] FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE
That last line that you typed is a condition that asks R to go through each item in score and tell whether that item is greater than 50 or not. It returns a list of TRUE and FALSE values depending on what’s in the vector. You can use this vector to select all the items from the original vector that are greater than 50:
> score[score > 50]
[1] 82 65 81 72 89 52 76
You can use conditions to separate the vector into two vectors: one whose values are less than the mean, one whose values are greater than or equal to the mean.
> mean(score) [1] 65.2 > small.score <- score[score < mean(score)] > big.score <- score[score >= mean(score)] > small.score [1] 47 65 39 49 52 > big.score [1] 82 81 72 89 76
From this example, you have probably figured out that < means “less than” and >= means “greater than or equal.” Here is a complete table:
| Symbol | Meaning |
|---|---|
| == | equals |
| != | not equal |
| < | less than |
| <= | less than or equal |
| > | greater than |
| >= | greater than or equal |
Important! If you want the “equals” condition, you must use two equal signs in a row. If you use only one, it won’t work!
Now let’s say you had a vector that told what gender each person taking your personality inventory was. From the output below, we see that the first person (who got a score of 47) was male, and the third person (who got a score of 65) was female.
> gender <- c("M", "M", "F", "F", "M", "F", "M", "M", "F", "F") > score [1] 47 82 65 81 39 72 89 49 52 76 > gender [1] "M" "M" "F" "F" "M" "F" "M" "M" "F" "F"
Now use a condition on the gender vector to tell which items to select from the score vector:
> male.score <- score[gender=="M"] > female.score <- score[gender=="F"] > male.score [1] 47 82 39 89 49 > female.score [1] 65 81 72 52 76
If you have been doing all these examples (and you have, haven’t you?!),
you have created quite a few objects. You can see what they are by listing them
with the ls() function. Remember to type the parentheses.
> ls()
[1] "big.score" "cHighs" "cLows" "female.score" "gender"
[6] "highs" "score" "lows" "male.score" "small.score"
[11] "try.me"
To remove objects, you use the rm() function. Give the names
of the objects, separated by commas. The following will get rid of the
try.me, cHighs, and cLows objects.
> rm(try.me, cHighs, cLows) > ls() # show that those items are really gone [1] "big.score" "female.score" "gender" "highs" "score" [6] "lows" "male.score" "small.score"
Figure out the following in R. See the solution.
mean(score) gives you the average, what do you think would
give you the median? Try it and find out.score that are below the value
in middleYou could exit R and save your work as you did before by typing q() to and then clicking the Yes button in the resulting dialog, but if you don’t mind typing, you can type this command to save and quit.
> q(save="yes")