2 Getting to know R data types

 

This chapter covers

  • Types of data you will likely encounter
  • How R stores data using types
  • How to assign values to variables

In any programming language, data is stored by the computer in memory using binary values. A binary value has only two possible states, and we can think of these as TRUE and FALSE, or more commonly, as 1 and 0. In computer memory, these represent the presence or absence of an electrical charge, so they are also ON and OFF.

We may see a 42 on the screen, but that’s thanks to the computer translating what it has stored into what we expect to see. This can be extremely useful (better than seeing 00000000 00000000 00000000 00101010) but also somewhat dangerous, as we’ll discuss later.

There are many different ways to store a piece of data in computer memory. By instructing the computer to treat data as a certain type, we can change how the data is manipulated.

2.1 Types of data

R is what’s known as a weakly typed language, in that the type of data is guessed or assumed rather than declared (or enforced). This can be very useful in terms of getting started because you don’t need to worry about setting the data type whenever you’re using data. But you’ll also learn how to change the type later if you aren’t getting what you want by default.

2.1.1 Numbers

2.1.2 Text (strings)

2.1.3 Categories (factors)

2.1.4 Dates and times

2.1.5 Logicals

2.1.6 Missing values

2.2 Storing values (assigning)

2.2.1 Naming data (variables)

2.2.2 Unchanging data

2.2.3 The assignment operators (<- vs. =)

2.3 Specifying the data type

2.4 Telling R to ignore something

2.5 Try it yourself

Terminology

Summary