 |
|
Vectors, matrixes and data.frames in R
Inspired by: Carsten Friis
Working with dimensional objects and indices
We will be storing our microarray data in objects in R. These objects will tend to get very big, given the nature of the microarrays ability to generate lots of numbers in parallel. To find the ups and downs of our data, you will find it helpful being able to navigate through indexed objects in R.
- Generate a vector x of 30 random numbers from the normal distribution using the function rnorm()
- Create a vector y that only contains the positive elements of x
- Create a vector z that censors the negative elements of x with zeros
- Create a matrix m with five columns and six rows from x using the matrix() function
Once you've created m, use the str() function on both m and x. See the difference...
- Convert m to a data.frame called d.f using the as.data.frame() function
Try using str() again on your new data.frame. A data.frame looks like a matrix, but it is different. Notice for example how columns now have names rather than just numbers. Columns in an R matrix can have names (see help(matrix) for how, look for the 'dimnames' property), but in a data.frame columns must have names.
R contains numerous as.[object]() functions. For example, as.vector(d.f) would give you a vector identical to your original x. These are very useful for converting between different object types, but conversion is not always this seamless.
- You can illustrate the difference between a matrix and a data.frame by setting one single element to a character in both m and d.f (e.g. m[2,2] = "a"; d.f[2,2] = "a")
Use str() again. The whole matrix m has been changed from a numeric object to a character object. In the data.frame, however, only the affected column has been changed. In R, a matrix is a matrix, but you should think of a data.frame more like a collection of named vectors. From a microarray perspective, data.frames allow us to keep annotation columns with gene-specific annotations within our data sets. On the other hand, matrices are often easier to work with...
|
|
This file was last modified Sunday 5th of October 2008 15:00:38 GMT |
|
|