R Programming/Objects
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Intro
- An object is just something that can be stored. It is defined by a set of properties.
- Vectors are the simplest R objects.
- Factors are similar to vectors but with a predetermined set of levels.
- A matrix is like a vector but with a specific instruction for the layout.
- Arrays are similar to matrix but can have more than 2 dimensions.
- A list is a vector of R objects.
- A dataframe is like a matrix but does not assume that all columns have the same type. A dataframe is a list of variables/vectors of the same length.
- Functions are also objects in R. See Programming Section
- Classes define how objects of a certain type look like.
- Classes are attached to object as an attribute
- All R objects have a class, a type and a dimension
>class(object) >type(object) >dim(object)
[edit] Vectors
Creating Vectors
> c(25,2,1,-3) # Combine elements into a vector [1] 25 2 1 -3 > rep(1,5) [1] 1 1 1 1 1 > numeric(5) # Creates a vector of zeros of length 5 [1] 0 0 0 0 0 > 1:5 [1] 1 2 3 4 5 > 4:-3 [1] 4 3 2 1 0 -1 -2 -3 > seq(1,10) [1] 1 2 3 4 5 6 7 8 9 10 > seq(1,5,by=.5) [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
Measuring the length of a vector
x = seq(1,5,by=.5) # Create a sequence of number x # Display this object [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 > length(x) # Get length of object x [1] 9
The rep() function creates vector repeating a pattern. For instance the following code will produce 5 repetitions of the (0,1) vector.
> x <- rep(c(0,1),5) > x [1] 0 1 0 1 0 1 0 1 0 1
> x<-1:5 > x [1] 1 2 3 4 5 > x <- seq(1,5,by=1) > x [1] 1 2 3 4 5
[edit] Factors
[edit] Matrix
- If you want to create a new matrix, one way is to use the matrix() function. You have to enter a vector of data, the number of rows and/or columns and finally you can specify if you want R to read your vector by row or by column (the default option). Here are two examples.
> matrix(data = NA, nrow = 5, ncol = 5, byrow = T)
[,1] [,2] [,3] [,4] [,5]
[1,] NA NA NA NA NA
[2,] NA NA NA NA NA
[3,] NA NA NA NA NA
[4,] NA NA NA NA NA
[5,] NA NA NA NA NA
> matrix(data = 1:15, nrow = 5, ncol = 5, byrow = T)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 1 2 3 4 5
[5,] 6 7 8 9 10
- Functions cbind() and rbind() combine vectors into matrices in a column by column or row by row mode:
> v1 <- 1:5
> v2 <- 5:1
> v2
[1] 5 4 3 2 1
> cbind(v1,v2)
v1 v2
[1,] 1 5
[2,] 2 4
[3,] 3 3
[4,] 4 2
[5,] 5 1
> rbind(v1,v2)
[,1] [,2] [,3] [,4] [,5]
v1 1 2 3 4 5
v2 5 4 3 2 1
- The dimension of a matrix can be obtained using the dim() function. Alternatively nrow() and ncol() returns the number of rows and columns in a matrix:
> dim(X) [1] 5 5 > nrow(X) [1] 5 > ncol(X) [1] 5
- Function t() transposes a matrix:
> X<-matrix(data = 1:15, nrow = 5, ncol = 5, byrow = T)
> t(X)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 1 6
[2,] 2 7 12 2 7
[3,] 3 8 13 3 8
[4,] 4 9 14 4 9
[5,] 5 10 15 5 10
[edit] Data Frames
A dataframe has been referred to as "a list of variables/vectors of the same length". In the following example, a dataframe of two vectors is created, each of five elements. The first vector, v1, is compose of a sequence of the integers 1 through 5. A second vector, v2, is composed of five logical values drawn of type T and F. The dataframe is then created, composed of the vectors.
> v1 = 1:5 > v2 = c(T,T,F,F,T) > df = data.frame(v1,v2) > print(df) v1 v2 1 1 TRUE 2 2 TRUE 3 3 FALSE 4 4 FALSE 5 5 TRUE
The dataframe may be created directly. In the following code, the dataframe is created - naming each vector composing the dataframe as part of the argument list.
> df = data.frame(foo=1:5,bar=c(T,T,F,F,T))> print(df) foo bar 1 1 TRUE 2 2 TRUE 3 3 FALSE 4 4 FALSE 5 5 TRUE
[edit] Arrays
An array is composed of n dimensions where each dimension is a vector of R objects of the same type. An array of one dimension of one element may be constructed as follows.
> x = array(c(T,F),dim=c(1)) > print(x) [1] TRUE
The array x was created with a single dimension (dim=c(1)) drawn from the vector of possible values c(T,F). A similar array, y, can be created with a single dimension and two values.
> a = array(c(T,F),dim=c(2)) > print(a) [1] TRUE FALSE
A three dimensional array - 3 by 3 by 3 - may be created as follows.
> z = array(1:27,dim=c(3,3,3))
> dim(z)
[1] 3 3 3
> print(z)
, , 1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
, , 2
[,1] [,2] [,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
, , 3
[,1] [,2] [,3]
[1,] 19 22 25
[2,] 20 23 26
[3,] 21 24 27
R arrays are accessed in a manner similar to arrays in other languages: by integer index, starting at 1 (not 0). The following code shows how the third dimension of the 3 by 3 by 3 array can be accessed. The third dimension is a 3 by 3 array.
> z[,,3]
[,1] [,2] [,3]
[1,] 19 22 25
[2,] 20 23 26
[3,] 21 24 27
Specifying two of the three dimensions returns an array on one dimension.
> z[,3,3] [1] 25 26 27
Specifying three of three dimension returns an element of the 3 by 3 by 3 array.
> z[3,3,3] [1] 27
More complex partionings of array may be had.
> z[,c(2,3),c(2,3)]
, , 1
[,1] [,2]
[1,] 13 16
[2,] 14 17
[3,] 15 18
, , 2
[,1] [,2]
[1,] 22 25
[2,] 23 26
[3,] 24 27
Arrays need not be symmetric across all dimensions. The following code creates a pair of 3 by 3 arrays.
> w = array(1:18,dim=c(3,3,2))
> print(w)
, , 1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
, , 2
[,1] [,2] [,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
Objects of the vectors composing the array must be of the same type by they need not be numbers.
> u = array(c(T,F),dim=c(3,3,2))
> print(u)
, , 1
[,1] [,2] [,3]
[1,] TRUE FALSE TRUE
[2,] FALSE TRUE FALSE
[3,] TRUE FALSE TRUE
, , 2
[,1] [,2] [,3]
[1,] FALSE TRUE FALSE
[2,] TRUE FALSE TRUE
[3,] FALSE TRUE FALSE
[edit] Lists
[edit] Convert objects
- as.matrix
- as.vector
- as.numeric
etc