R programming -- Metrix

  1. Matrices

    • Try R is Sponsored By:
      O'Reilly
    • Complete to
      Unlock
      Chapter 3 Badge

    So far we've only worked with vectors, which are simple lists of values. What if you need data in rows and columns? Matrices are here to help.

    A matrix is just a fancy term for a 2-dimensional array. In this chapter, we'll show you all the basics of working with matrices, from creating them, to accessing them, to plotting them.

  2. Matrices3.1

    Let's make a matrix 3 rows high by 4 columns wide, with all its fields set to 0.

    Redo Complete
    > matrix(0, 3, 4)
          [,1] [,2] [,3] [,4]
    [1,]    0    0    0    0
    [2,]    0    0    0    0
    [3,]    0    0    0    0
    
  3. You can also use a vector to initialize a matrix's value. To fill a 3x4 matrix, you'll need a 12-item vector. We'll make that for you now:

    Redo Complete
    > a <- 1:12
    
  4. If we print the value of a, we'll see the vector's values, all in a single row:

    Redo Complete
    > print(a)
      [1]  1  2  3  4  5  6  7  8  9 10 11 12
    
  5. Now call matrix with the vector, the number of rows, and the number of columns:

    Redo Complete
    > matrix(a, 3, 4)
          [,1] [,2] [,3] [,4]
    [1,]    1    4    7   10
    [2,]    2    5    8   11
    [3,]    3    6    9   12
    
  6. The vector's values are copied into the new matrix, one by one. You can also re-shape the vector itself into a matrix. We'll create a new 8-item vector for you:

    Redo Complete
    > plank <- 1:8
    
  7. The dim assignment function sets dimensions for a matrix. It accepts a vector with the number of rows and the number of columns to assign.

    Assign new dimensions to plank by passing a vector specifying 2 rows and 4 columns (c(2, 4)):

    Redo Complete
    > dim(plank) <- c(2, 4)
    
  8. If you print plank now, you'll see that the values have shifted to form 2 rows by 4 columns:

    Redo Complete
    > print(plank)
          [,1] [,2] [,3] [,4]
    [1,]    1    3    5    7
    [2,]    2    4    6    8
    
  9. The vector is no longer one-dimensional. It has been converted, in-place, to a matrix.

    Now, use the matrix function to make a 5x5 matrix, with its fields initialized to any values you like.

    Redo Complete
    > matrix(1, 5, 5)
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    1    1    1    1
    [2,]    1    1    1    1    1
    [3,]    1    1    1    1    1
    [4,]    1    1    1    1    1
    [5,]    1    1    1    1    1
    
  10. Matrix Access3.2

    Getting values from matrices isn't that different from vectors; you just have to provide two indices instead of one.

    Let's take another look at our plank matrix:

    Redo Complete
    > print(plank)
          [,1] [,2] [,3] [,4]
    [1,]    1    3    5    7
    [2,]    2    4    6    8
    
  11. Try getting the value from the second row in the third column of plank:

    Redo Complete
    > plank[2, 3]
    [1] 6
    
  12. Now, try getting the value from first row of the fourth column:

    Redo Complete
    > plank[1, 4]
    [1] 7
    
  13. As with vectors, to set a single value, just assign to it. Set the previous value to 0:

    Redo Complete
    > plank[1, 4] <- 0
    
  14. You can get an entire row of the matrix by omitting the column index (but keep the comma). Try retrieving the second row:

    Redo Complete
    > plank[2,]
    [1] 2 4 0 8
    
  15. To get an entire column, omit the row index. Retrieve the fourth column:

    Redo Complete
    > plank[, 4]
    [1] 7 8
    
  16. You can read multiple rows or columns by providing a vector or sequence with their indices. Try retrieving columns 2 through 4:

    Redo Complete
    > plank[, 2:4]
          [,1] [,2] [,3]
    [1,]    3    5    7
    [2,]    4    0    8
    
  17. Matrix Plotting3.3

    Text output is only useful when matrices are small. When working with more complex data, you'll need something better. Fortunately, R includes powerful visualizations for matrix data.

    We'll start simple, with an elevation map of a sandy beach.

    It's pretty flat - everything is 1 meter above sea level. We'll create a 10 by 10 matrix with all its values initialized to 1 for you:

    Redo Complete
    > elevation <- matrix(1, 10, 10)
    
  18. Oh, wait, we forgot the spot where we dug down to sea level to retrieve a treasure chest. At the fourth row, sixth column, set the elevation to 0:

    Redo Complete
    > elevation[4, 6] <- 0
    
  19. You can now do a contour map of the values simply by passing the matrix to the contour function:

    Redo Complete
    > contour(elevation)
    
    • 0.00.20.40.60.81.00.00.20.40.60.81.0
  20. Or you can create a 3D perspective plot with the persp function:

    Redo Complete
    > persp(elevation)
    
    • elevationYZ
  21. The perspective plot looks a little odd, though. This is because persp automatically expands the view so that your highest value (the beach surface) is at the very top.

    We can fix that by specifying our own value for the expand parameter.

    Redo Complete
    > persp(elevation, expand=0.2)
    
    • elevationYZ
  22. Okay, those examples are a little simplistic. Thankfully, R includes some sample data sets to play around with. One of these is volcano, a 3D map of a dormant New Zealand volcano.

    It's simply an 87x61 matrix with elevation values, but it shows the power of R's matrix visualizations.

    Try creating a contour map of the volcano matrix:

    Redo Complete
    > contour(volcano)
    
    • 0.00.20.40.60.81.00.00.20.40.60.81.0
  23. Try a perspective plot (limit the vertical expansion to one-fifth again):

    Redo Complete
    > persp(volcano, expand=0.2)
    
    • volcanoYZ
  24. The image function will create a heat map:

    Redo Complete
    > image(volcano)
    
    • 0.00.20.40.60.81.00.00.20.40.60.81.0
  25. Chapter 3 Completed

    Chapter 3 Badge
    Share your plunder:

    Here we stand on the beach, at the end of Chapter 3. What's this, buried in the sand? It's another badge!

    In this chapter, we learned how to create matrices from scratch, and how to re-shape a vector into a matrix. We learned how to access values within a matrix one-by-one, or in groups. And we saw just a few of the ways to visualize a matrix's data.

    None of the techniques we've used so far will help you describe your data, though. We'll rectify that in the next chapter, where we'll talk about summary statistics.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值