Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

[1,] 5 10 15 20 25

[2,] 30 35 40 45 50
[3,] 55 60 65 70 75
[4,] 80 85 90 95 100
The function matrix() provides another way to create matrices:
> num_matrix <- matrix(seq(5,100,5),nrow=5)
> num_matrix
[,1] [,2] [,3] [,4]
[1,] 5 30 55 80
[2,] 10 35 60 85
[3,] 15 40 65 90
[4,] 20 45 70 95
[5,] 25 50 75 100
If you add the argument byrow=T , R fills the matrix by rows, like this:
> num_matrix <- matrix(seq(5,100,5),nrow=5,byrow=T)
> num_matrix
[,1] [,2] [,3] [,4]
[1,] 5 10 15 20
[2,] 25 30 35 40
[3,] 45 50 55 60
[4,] 65 70 75 80
[5,] 85 90 95 100
How do you refer to a particular matrix component? you type the matrix name and
then, in brackets, the row number, a comma, and the column number:
> num_matrix[5,4]
[1] 100
Factors
In Chapter 1, I describe four types of data: nominal, ordinal, interval, and ratio.
In
nominal data, numbers are just labels, and their magnitude has no significance.
Suppose you’re doing a survey of people’s eye color. As you record a person’s eye
color, you record a number: 1 = amber, 2 = blue, 3 = brown, 4 = gray, 5 = green,
and 6 = hazel. One way to think of this process is that eye color is a factor, and
each
color is a level of that factor. So in this case, the factor eye-color has six
levels.

You might also like