Introduction to Character Vectors in R

Vectors in R can be used to store characters or text data. This post will go over some basic ways you can use character vectors.

You create character vectors in much the same way as numeric vectors. The only main difference is that you put quotes ” ” around the words. The quotes tell R that the information within the quotes is text and not numeric information. Below is an example of the creation of a character vector consisting of the names of students.

  • > student.names <- c("Andy", "Billy", "Chris", "Darrin", "Ed", "Frank", "Gabe", "Hank", "Ivan", "James", "Karl", "Larry", "Matt", "Norman", "Oscar", "Paul", "Quinton")
    > student.names
     [1] "Andy"    "Billy"   "Chris"   "Darrin"  "Ed"      "Frank"   "Gabe"    "Hank"   
     [9] "Ivan"    "James"   "Karl"    "Larry"   "Matt"    "Norman"  "Oscar"   "Paul"   
    [17] "Quinton

In this example, we assigned the ‘student.names’ variable to the names and then we typed the variable named ‘student.names’ so that R would print the results.

Subsetting Values

A subset is the result of pulling some information from a larger vector. This is useful if you want to work with a piece of data from a larger data set. For example, let’s say you only want the fourth name from the ‘student.names’ variable to do this an example is provided.

  • > student.names[4]
    [1] "Darrin"

All you did in the example above was type in the variable ‘student.names’. Next you placed in brackets [ ] the number four. This tells R you only want the information in the fourth index of the vector. R then prints the result with the fourth name of the vector.

The possibilities with subsets are endless. For example, you can ask for a sequence of indicies using a colon as shown in the example below where we ask for the fourth through eighth names in the vector.

> student.names[4:8]
[1] "Darrin" "Ed"     "Frank"  "Gabe"   "Hank"

Creating and Assigning Named Vectors

Vectors can be assigned to other vectors. This is used when you need to combine information from different vectors in a way that certain information is linked with certain information. For example, what if you want to combine the students’ names with their test scores? Below is an example of how to do this.

> student.names <- c("Andy", "Billy", "Chris", "Darrin", "Ed", "Frank", "Gabe", "Hank", "Ivan", "James", "Karl", "Larry", "Matt", "Norman", "Oscar", "Paul", "Quinton")
> test.scores <- c(85, 90, 80, 95, 60, 55, 72, 88, 71, 82, 62, 58, 89, 76, 64, 79, 88) 
> names(test.scores) <- student.names 
> test.scores  
    Andy Billy Chris Darrin Ed Frank Gabe Hank Ivan James Karl 
     85     90    80   95    60 55    72    88  71    82  62 
    Larry Matt Norman Oscar Paul Quinton 
     58    89   76      64   79    88

Here is what happened. NOTE: Each name should have a number under it. The formatting is difficult in WordPress sometimes

  1. We created are ‘student.names’ variable.
  2. We created are ‘test.scores’ variable.
  3. We then assigned the names or values in ‘test.score’ to the variable ‘student.names’. This  basic gives a name to every value in the ‘test.score’ variable. In other words, the first value 85 now has the name Andy, the second value 90 now has the name Billy.
  4. You now type ‘test.scores’ again to print the new combined vector.

Now every score has a named associated with it. Think of the name as another form of identification. You can subset information using the index or the name as shown below.

> test.scores["Andy"]
> test.scores[1]

Both examples above give you the score for the first index which is 85.

This is just an introduction to character vectors. Keep in mind that much of this information applies to numeric vectors as well.


3 thoughts on “Introduction to Character Vectors in R

  1. Pingback: Introduction to Factors in R | educationalresearchtechniques

  2. Pingback: Introduction to Character Vectors in R | Educat...

  3. Pingback: Plotting Correlations in R | educationalresearchtechniques

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s