You are here

More Basics in R

Let's create a variable with a series of data points:

HHsize<-c(2,3,2,1,4,5,1,1,2,5,1,2,2,6,1,2,2,4)

We've created a vector to hold these data points. You can imagine, as we discussed, that each data point is in a box, and the vector is a stack of boxes. It doesn't matter if the boxes are stacked vertically or horizontally.

We can refer to a specific box by using the square brackets, [ ]

HHsize[3]

This refers to the third data point in the vector, which in this case, has a value of 2.

You can identify boxes by their position, as above, or by posing questions about their value. In other words, you can put a question in the square brackets and R will identify the data points for which the question is true. (This requires R to create an use a logical vector.)

HHsize[HHsize>3]

R doesn't use punctuation -- a question mark -- to identify a question, but when you put such a statement in the square brackets, you are asking R a TRUE/FALSE question: Is the value of this data point greater than 3?

We will use this syntax throughout the semester to do recoding. More on that later.