Skip to main content

2.4) Classes of Data Vectors


Importing a data set and examining it with str() may reveal several classes of vector in the resulting data frame. In addition to integer and numeric vectors, there can be factors. These are automatically produced if a column contains any letters (other than the value NA), and they ensure the variable will be treated as discrete categories by functions like aov() (for doing an Analysis of Variance). The output of a factor when printed on the screen is always accompanied by a string of the different levels of the factor. If you need to ensure a vector is treated as a factor, apply the function factor() to it: e.g. my.factor <- factor(rep(1:3,each=2)). Factors can be converted to plain character vectors using as.character(). Another class is "logical", allowing only the values TRUE and FALSE (or T and F). We will soon see how useful this is (section 2.7)...