Modes and Classes
It was mentioned earlier that all the elements of a vector must be of the same mode. To see the mode of an object, you can use the mode function. What happens if we try to combine objects of different modes using the c function? The answer is that R will find a common mode that can accomodate all the objects, resulting in the mode of some of the objects changing. For example, let's try combining some numbers and some character strings:
You can see that the numbers have been changed to characters because they are now displayed surrounded by quotes. They also will no longer behave like numbers:
Let's call this matrix M.7 (a valid name in R) and look at the multiples of it. Matrix multiplication in R is the%.% symbol, not the. symbol. (. does entry-by-entry multiplication, which is good for convolution but not for this.) Look what happens when you multiply M.7 by itself: it starts to cascade. But how does R know how big the matrix must be? If we check the command we can see that we provide an integer vector with 9 elements, and ask for a matrix with 3 rows (nrow = 3). There is only one way to fulfill the requirements: creating a (3 times 3 ) matrix. When we look at the output above we can also see how the values have been filled in. I am new to matrix operations using R and I need your help to create a matrix containing 0's and 1's using the results of the granger test of the below data set (Kmn2010) containing returns of st.
The error message means that the two values can no longer be added. If you really need to treat character strings like numbers, you can use the as.numeric function:
Of course, the best thing is to avoid combining objects of different modes with the c function. We'll see later that R provides an object known as a list that can store different types of objects without having to change their modes.
Reading Vectors
Once you start working with larger amounts of data, it becomes very tedious to enter data into the c function, especially considering the need to put quotes around character values and commas between values. To read data from a file or from the terminal without the need for quotes and commas, you can use the scan function. To read from a file (or a URL), pass it a quoted string with the name of the file or URL you wish to read; to read from the terminal, call scan() with no arguments, and enter a completely blank line when you're done entering your data. Additionally, on Windows or Mac OS X, you can substitute a call to the file.choose() function for the quoted string with the file name, and you'll be presented with the familiar file chooser used by most programs on those platforms.
Suppose there's a file called numbers in your working directory. (You can get your working directory by calling the getwd() function, or set it using the setwd function or File -> Change dir selection in the R console.) Let's say the contents of this file looks like this:
The scan function can be used to read these numbers as follows:
The optional what= argument to scan can be used to read vectors of character or logical values, but remember a vector can only hold objects all of which are of the same mode.
Missing Values
No matter how carefully we collect our data, there will always be situations where we don't know the value of a particular variable. For example, we might conduct a survey where we ask people 10 questions, and occasionally we forget to ask one, or people don't know the proper answer. We don't want values like this to enter into calculations, but we can't just eliminate them because then observations that have missing values won't 'fit in' with the rest of the data.
In R, missing values are represented by the string NA. For example, suppose we have a vector of 10 values, but the fourth one is missing. I can enter a missing value by passing NA to the c function just as if it was a number (no quotes needed):
R will also recognize the unquoted string NA as a missing value when data is read from a file or URL.
Missing values are different from other values in R in two ways:
Create Matrix In R
- Any computation involving a missing value will return a missing value.
- Unlike other quantities in R, we can't directly test to see if something is equal to a missing value with the equality operator (). We must use the builtin is.na function, which will return TRUE if a value is missing and FALSE otherwise.
Here are some simple R statements that illustrate these points:
Fortunately, these problems are fairly easy to solve. In the first case, many functions (like mean, min, max, sd, quantile, etc.) accept an na.rm=TRUE argument, that tells the function to remove any missing values before performing the computation:
In the second case, we just need to remember to always use is.na whenever we are testing to see if a value is a missing value.
By combining a call to is.na to the logical 'not' operator (!) we can filter out missing values in cases where no na.rm= argument is available:
Inverse Matrix In R
Matrices
A very common way of storing data is in a matrix, which is basically a two-way generalization of a vector. Instead of a single index, we can use two indexes, one representing a row and the second representing a column. The matrix function takes a vector and makes it into a matrix in a column-wise fashion. For example,
The last two arguments to matrix tell it the number of rows and columns the matrix should have. If you used a named argument, you can specify just one dimension, and R will figure out the other:
To create a matrix by rows instead of by columns, the byrow=TRUE argument can be used:
When data is being read from a file, you can simply imbed a call to scan into a call to matrix. Suppose we have a file called matrix.dat with the following contents:
We could create a 3×4 matrix, read in by rows, with the following command:
To access a single element of a matrix, we need to specify both the row and the column we're interested in. Consider the following matrix, containing the numbers from 1 to 10:
Now suppose we want the element in row 4 and column 1:
Create Correlation Matrix In R
If we leave out either one of the subscripts, we'll get the entire row or column of the matrix, depending on which subscript we leave out:
Data Frames
One shortcoming of vectors and matrices is that they can only hold one mode of data; they don't allow us to mix, say, numbers and character strings. If we try to do so, it will change the mode of the other elements in the vector to conform. For example:
Notice that the numbers got changed to character values so that the vector could accomodate all the elements we passed to the c function. In R, a special object known as a data frame resolves this problem. A data frame is like a matrix in that it represents a rectangular array of data, but each column in a data frame can be of a different mode, allowing numbers, character strings and logical values to coincide in a single object in their original forms. Since most interesting data problems involve a mixture of character variables and numeric variables, data frames are usually the best way to store information in R. (It should be mentioned that if you're dealing with data of a single mode, a matrix may be more efficient than a data frame.) Data frames correspond to the traditional 'observations and variables' model that most statistical software uses, and they are also similar to database tables. Each row of a data frame represents an observation; the elements in a given row represent information about that observation. Each column, taken as a whole, has all the information about a particular variable for the data set. Iconvert icons 2 9 – convert icons to png format.
For small datasets, you can enter each of the columns (variables) of your data frame using the data.frame function. For example, let's extend our temperature example by creating a data frame that has the day of the month, the minimum temperature and the maximum temperature:
Transpose Matrix In R
Note that the names we used when we created the data frame are displayed with the data. (You can add names after the fact with the names function.) Also, instead of typing the name temps to see the data frame, we used a call the the head function instead. This will show me just the first six observations (by default) of the data frame, and is very handy to check to make sure a large data.frame really looks the way you think. (There's a function called tail that shows the last lines in an object as well.)
R will also recognize the unquoted string NA as a missing value when data is read from a file or URL.
Missing values are different from other values in R in two ways:
Create Matrix In R
- Any computation involving a missing value will return a missing value.
- Unlike other quantities in R, we can't directly test to see if something is equal to a missing value with the equality operator (). We must use the builtin is.na function, which will return TRUE if a value is missing and FALSE otherwise.
Here are some simple R statements that illustrate these points:
Fortunately, these problems are fairly easy to solve. In the first case, many functions (like mean, min, max, sd, quantile, etc.) accept an na.rm=TRUE argument, that tells the function to remove any missing values before performing the computation:
In the second case, we just need to remember to always use is.na whenever we are testing to see if a value is a missing value.
By combining a call to is.na to the logical 'not' operator (!) we can filter out missing values in cases where no na.rm= argument is available:
Inverse Matrix In R
Matrices
A very common way of storing data is in a matrix, which is basically a two-way generalization of a vector. Instead of a single index, we can use two indexes, one representing a row and the second representing a column. The matrix function takes a vector and makes it into a matrix in a column-wise fashion. For example,
The last two arguments to matrix tell it the number of rows and columns the matrix should have. If you used a named argument, you can specify just one dimension, and R will figure out the other:
To create a matrix by rows instead of by columns, the byrow=TRUE argument can be used:
When data is being read from a file, you can simply imbed a call to scan into a call to matrix. Suppose we have a file called matrix.dat with the following contents:
We could create a 3×4 matrix, read in by rows, with the following command:
To access a single element of a matrix, we need to specify both the row and the column we're interested in. Consider the following matrix, containing the numbers from 1 to 10:
Now suppose we want the element in row 4 and column 1:
Create Correlation Matrix In R
If we leave out either one of the subscripts, we'll get the entire row or column of the matrix, depending on which subscript we leave out:
Data Frames
One shortcoming of vectors and matrices is that they can only hold one mode of data; they don't allow us to mix, say, numbers and character strings. If we try to do so, it will change the mode of the other elements in the vector to conform. For example:
Notice that the numbers got changed to character values so that the vector could accomodate all the elements we passed to the c function. In R, a special object known as a data frame resolves this problem. A data frame is like a matrix in that it represents a rectangular array of data, but each column in a data frame can be of a different mode, allowing numbers, character strings and logical values to coincide in a single object in their original forms. Since most interesting data problems involve a mixture of character variables and numeric variables, data frames are usually the best way to store information in R. (It should be mentioned that if you're dealing with data of a single mode, a matrix may be more efficient than a data frame.) Data frames correspond to the traditional 'observations and variables' model that most statistical software uses, and they are also similar to database tables. Each row of a data frame represents an observation; the elements in a given row represent information about that observation. Each column, taken as a whole, has all the information about a particular variable for the data set. Iconvert icons 2 9 – convert icons to png format.
For small datasets, you can enter each of the columns (variables) of your data frame using the data.frame function. For example, let's extend our temperature example by creating a data frame that has the day of the month, the minimum temperature and the maximum temperature:
Transpose Matrix In R
Note that the names we used when we created the data frame are displayed with the data. (You can add names after the fact with the names function.) Also, instead of typing the name temps to see the data frame, we used a call the the head function instead. This will show me just the first six observations (by default) of the data frame, and is very handy to check to make sure a large data.frame really looks the way you think. (There's a function called tail that shows the last lines in an object as well.)
If we try to look at the class or mode of a data frame, it's not that informative:
We'll see the same results for every data frame we use. To look at the modes of the individual columns of a data frame, we can use the sapply function. This function simplifies operations that would require loops in other languages, and automatically returns the appropriate results for the operation it performs. To use sapply on a data frame, pass the data frame as the first argument to sapply, and the function you wish to use as the second argument. So to find the modes of the individual columns of the temps data frame, we could use
Notice that sapply even labeled the result with the name of each column.
Suppose we want to concentrate on the maximum daily temperature (which we've called max in our data frame) among the days recorded. There are several ways we can refer to the columns of a data frame:
- Probably the easiest way to refer to this column is to use a special notation that eliminates the need to put quotes around the variable names (unless they contain blanks or other special characters). Separate the data frame name from the variable name with a dollar sign ($):
- We can treat the data frame like it was a matrix. Since the maximum temperature is in the third column, we could say
- Since we named the columns of temps we can use a character subscript:
- When you use a single subscript with a data frame, it refers to a data frame consisting of just that column. R also provides a special subscripting method (double brackets) to extract the actual data (in this case a vector) from the data frame:
Notice that this second form is identical to temps$max. We could also use the equivalent numerical subscript (in this case 3) with single or double brackets.