The purpose of this chapter is to show the novice R user how R stores data by introducing the shortcuts that make data entry a fairly simple task.
Entering screeds of data is not fun in any software tool. It’s more common for the R user to have the data they need already available from another source. This is covered in Chapter 4 on importing data from alternate sources.
Of course, if you really must enter data manually then you’d better read on; we can at least try to make it as painless as possible.
R can be used to do basic operations whose results do not get stored as objects. We can also assign the answers to a variable. For example
This means we can use the variable by name later. For example
We can also use some basic mathematical functions such as the logarithm and square root via the log() and sqrt() commands — many other commands like this exist! For example
OK, these manipulations are trivial, but they can be used in conjunction with other data objects as we will see later. More detail on how to use R as a scientific calculator can be found in Chapter 17.
Operating on single values is rare. We are usually faced with numbers that we wish to use as a set. Entering them as a set is therefore necessary. The most basic way of entering a set of numbers is using the c() command. For example
is the list of the squares of the first ten natural numbers. We can obtain the numbers from 1 to 10 faster by issuing
and therefore can obtain the desired set of the squares for the first ten natural numbers using shorter code based on a simple sequence.
This is much more efficient than typing out the actual results as done previously. Note that the colon symbol is used for generating series of integers and that in terms of the order of mathematical operators, it comes after the exponent; the brackets around the sequence are essential. In this case, squaring a number is achieved through use of the carat symbol followed by a 2.
The c() command is good for entering any kind of data. We may need to enter a set of categories for example.
Note two important features of this command. I capitalized the variable name here on purpose. The vast majority of R commands are in lower case and there is one called “names". I don’t want to confuse my data and an existing R command so my preference is to use an upper case letter on the front of all my variable names that mean anything. The second point is that the character-valued data I entered were encapsulated by quote marks. This means that the actual names (mine, my last dog and some family members) were stored. If I had omitted the quote marks, R would have looked for variables with the appropriate names — not defined in this instance. You could remove a quote or two to see what happens if you must.
R often uses logical indicators to tell us something. These variables take the values “TRUE" or “FALSE".
You might suspect that this variable tells you if the name given in the Names variable are for humans. The fourth name is therefore the dog. To extract the names of the humans we use the command
Note here that the brackets used are square brackets. Use round brackets for functions, square for elements of an object.
It’s pretty simple to extract the dog’s name:
In this situation the exclamation mark should be read as “not" and therefore picks up the elements where the logical variable is set to FALSE.
We’ve seen that we can find a subset of the set of names using the indicator variable, but it’s frequently useful to be able to extract one or more elements by their location. For example
gives the first name, and
extracts the second and third names. The set of names entered is a vector and has only one subscript to monitor. We will see how to subscript elements within a matrix or data.frame later.
It’s also often necessary to understand how R has stored an object. The class() command is useful, and so is the str() command. For example
We can get the names of all the people that aren’t me by
which of course assumes you know my name was given first. The subscripts have used square brackets in these examples. The type (and number) of brackets is crucial. If you open a bracket it must be closed, and closed by a bracket of the same type. Nesting brackets is quite acceptable.
In many instances we need to generate series of values in a patterned way. Let’s say we want to generate variables that represent the twenty working days over a four week period. We want a list of the week number, and then a list of the weekday names. In both situations we will use the rep() command. It has three arguments; the list of values, the number of times each value is to be repeated, and the number of times the whole series should be repeated. The second and third of these arguments have default values so may not need to be stated explicitly.
These two variables can be brought together with the corresponding data using the data.frame() command illustrated later in this chapter.
The rep() command is very flexible, and to be honest can either be a lot of fun to play with or just one big headache. Let’s say we want to generate the series of numbers which has one 1, two 2’s, three 3’s, four 4’s, and five 5’s. Instead of using the constant for the number of times each element is repeated, we can choose the number of repeats for each element.
Whatever you do you should observe the way your series is coming out. I would have expected the each to be used in the last example not times for example — always check.
Let’s say we want a set of numbers to be cycled, but know that we won’t use the full cycle. The rep() has an argument that can stop the process early for us. Try using the length.out argument as follows
The current time and date can be extracted using the date() command but this is not an object that can be manipulated — it is a character string only.
It might be useful to print this information as part of your documentation of an analysis. We can come back to an R session at a later time and to keep track of when we do things might prove useful. You can see from the output given here the exact time and date this document was compiled. This format is not to be confused with what we would store or manipulate; it is just a print out of the current time and date. The Sys.Date() command stores the same information as number.
This print out is different to what needs to happen when we want to store numerical values that represent the times and dates particular observations were taken. The base distribution of R does not cater for extracting the date and time in simple numeric terms. This can be achieved, but is beyond the scope of this chapter. It may prove best to store a date using its three constituent parts (day, month, and year) as separate numeric variables. Times should be stored using 24-hour format and be careful not to use a separator between the hour and minute values. Mathematical operations should not be done on these variables unless we convert the minutes to decimal fractions of an hour. In any situation you should decide what you will do with the data before choosing the format you wish to store it in.
An example for storing details of months might be useful here.
The as.factor() command tells R that these numbers are to be thought of as non-numeric data. A factor also has an associated attribute called levels. We can edit the levels directly and this will change our entire variable.
As it happens, we didn’t need to actually explicitly state the month number when the variable was first created, but it is good practice to keep things logical!
There are two data object types that are quite similar but not the same. A matrix is a two-dimensional array of values of the same type — numeric, character, or logical. A data.frame looks like a matrix but can have variables of different types embedded within it. For example, we can create a new data.frame by combining the names and human status variables created earlier using
We would usually assign the results of this command to a named object for storage.
Now we can see why we should not confuse names and Names. We can ask for the names of the variables in a data.frame using the names() command. For example
We can also now think about how we might extract the fourth name from the data.frame because this is the data structure we will work with the most. There are several alternatives.
Notice that as well as the result we wanted, R has also printed the levels of the Names variable. This is because this variable has been determined to be a factor.
The construction of our first data.frame is slightly flawed. If the MyFirstDF data was going to be for all those beings I am in contact with, the details should be related to the individual concerned. In this situation, allowing the Names object to be data rather than a label was probably not the wisest move. Let’s say that we want the year and month of birth for the individuals in the example, and that they should form a new data.frame.
Data entry is tedious. Efficiency is therefore an important weapon in your armoury. When we plan experiments we are often interested in obtaining an observation (or multiple observations) for every combination of some factors. In this simple example, imagine there are three experimental factors, given the names H, W, and Sex, and that each factor can take either of two levels. The expand.grid() command is a useful way to construct a data.frame.
I’ve used a full printout of the resulting data.frame instead of using the str() command to show what R has created because the str() command gives additional information that we do not require at this point.
Let’s say we wish to add a new variable to this data.frame; a task common when designing an experiment. We can use the $ notation shown earlier in a new way. We add a set of eight random values extracted from a standard normal distribution here as an illustration using the rnorm() function.
I like to create random data when planning an analysis. In this instance, the data are normally distributed which is not particularly important, but they are random which is important. There are many other functions that generate random data from other distributions; by convention, these functions all start with a letter “r" followed by a shortened form of the distribution’s name.
I do this because data that will be collected should be appropriate for the intended analysis to be conducted, and likewise the analysis should reflect the way data were collected. Creation of the random data means I can write the R commands that will generate the analysis, and once I have checked that the analysis is possible, I can then collect the data. When the data has been collected, I can then re-process the commands using the real data instead of the random data. This can save time during the analysis but more importantly, I can be confident that the data collection and analysis were planned well.