Contents
 
Introduction
To R

Characters and Factors

Characters

R can store character data in the same way as we have been storing numeric data. Character data is enclosed in either single quote marks ' or double quote marks ". Use either, as long as they match! Examples:
> c1 <- "Hello"   A scalar character string
> c1
[1] "Hello"

> c2 <- c("Yes",'Maybe',"No")  A vector of characters
> c2
[1] "Yes"   "Maybe" "No"

> c3 <- c("Is","Could Be","Isn't") Note single quote inside doubles

You can also use some of the functions we used to construct numeric vectors with character data:
> c3 <- rep("Monkey",4)       Using rep()
> c3
[1] "Monkey" "Monkey" "Monkey" "Monkey"

> c4 <- rep(c("Monkey","Bucket"),3)   
> c4
[1] "Monkey" "Bucket" "Monkey" "Bucket" "Monkey" "Bucket"
Of course, don't expect to be able to add characters!

Factors

A factor is used for storing categorical data. Suppose you have data on some people and you want to store their sex. You could use a numeric code - 0 for male, 1 for female, or you could use a character code - `M' for male and `F' for female. But you should really use a factor.

Constructing factors

Factors are easy to construct from character vectors. Use the function as.factor with a character vector:
> c5 <- c('M','F','F','F','M','M')
> f5 <- as.factor(c5)
> f5
[1] M F F F M M
Levels:  F M
Notice that a factor gets printed out like a character vector, but without the quote marks. The levels are also printed out.

The categories in a factor can also be seen by using the levels() function:

> levels(f5)
[1] "F" "M"      The result is a character vector
And you can change these levels easily by assigning a new character vector to the levels:
> levels(f5)_c("Female","Male")
> f5
[1] Male   Female Female Female Male   Male
Levels:  Female Male
One thing you can do with a factor is to tabulate the counts of each category with the table() function:
> table(f5)
Female   Male
     3      3       3 of each!

Contents