| Please choose a course, or type 0 to exit swirl.
1: R Programming 2: Take me to the swirl course repository!
Selection: 1
| Please choose a lesson, or type 0 to return to course menu.
1: Basic Building Blocks 2: Workspace and Files 3: Sequences of Numbers 4: Vectors 5: Missing Values 6: Subsetting Vectors 7: Matrices and Data Frames 8: Logic 9: Functions 10: lapply and sapply 11: vapply and tapply 12: Looking at Data 13: Simulation 14: Dates and Times 15: Base Graphics
Selection: 7
| | 0%
| In this lesson, we | represent | store tabular data, with rows and columns.
...
|== | 3%
| The main difference, as you | contain a single class of data, while data frames can consist of | many different classes of data.
...
|=== | 6%
| Let | `:` operator. Store the result in a variable called my_vector.
> my_vector<-1:20
| That
|===== | 9%
| View the contents of the vector you just created.
> my_vector [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Excellent job!
|======= | 11%
| The dim() function tells us the | happens if we do dim(my_vector)? Give it a try.
> dim(my_vector) NULL
| That
|========= | 14%
| Clearly, that | doesn | its length using the length() function. Try that now.
> length(my_vector) [1] 20
| Excellent job!
|========== | 17%
| Ah! That | `dim` attribute? Let [1] 4 5
| All that practice is paying off!
|=============== | 26%
| Another way to see this is by calling the attributes() function on | my_vector. Try it now.
> attributes(my_vector) $dim [1] 4 5
| Keep up the great work!
|================= | 29%
| Just like in math class, when dealing with a 2-dimensional object | (think rectangular table), the first number is the number of rows | and the second is the number of columns. Therefore, we just gave | my_vector 4 rows and 5 columns.
...
|=================== | 31%
| But, wait! That doesn | not. Now it | what it looks like.
> my_vector [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20
| All that practice is paying off!
|===================== | 34%
| Now, let | function. Type class(my_vector) to see what I mean.
> class(my_vector) [1] "matrix"
| You
|====================== | 37%
| Sure enough, my_vector is now a matrix. We should store it in a new | variable that helps us remember what it is. Store the value of | my_vector in a new variable called my_matrix.
> my_matrix<-my_vector
| Keep up the great work!
|======================== | 40%
| The example that we | point that a matrix is simply an atomic vector with a dimension | attribute. A more direct method of creating the same matrix uses | the matrix() function.
...
|========================== | 43%
| Bring up the help file for the matrix() function now using the `?` | function.
> ?matrix
| Perseverance, that
|=========================== | 46%
| Now, look at the documentation for the matrix function and see if | you can figure out how to create a matrix containing the same | numbers (1-20) and dimensions (4 rows, 5 columns) by calling the | matrix() function. Store the result in a variable called | my_matrix2.
> my_matrix2<-matrix(1:20,nrow=4,ncol=5)
| That
|============================= | 49%
| Finally, let | identical. The identical() function will tell us if its first two | arguments are the same. Try it out.
> identical(my_matrix,my_matrix2) [1] TRUE
| You nailed it! Good job!
|=============================== | 51%
| Now, imagine that the numbers in our table represent some | measurements from a clinical experiment, where each row represents | one patient and each column represents one variable for which | measurements were taken.
...
|================================= | 54%
| We may want to label the rows, so that we know which numbers belong | to each patient in the experiment. One way to do this is to add a | column to the matrix, which contains the names of all four people.
...
|================================== | 57%
| Let | our patients -- Bill, Gina, Kelly, and Sean. Remember that double | quotes tell R that something is a character string. Store the | result in a variable called patients.
> patients<-c("Bill","Gina","Kelly","Sean")
| You are doing so well!
|==================================== | 60%
| Now we | worry about storing the result in a new variable. Just call cbind() | with two arguments -- the patients vector and my_matrix.
> cbind(patients,my_matrix) patients [1,] "Bill" "1" "5" "9" "13" "17" [2,] "Gina" "2" "6" "10" "14" "18" [3,] "Kelly" "3" "7" "11" "15" "19" [4,] "Sean" "4" "8" "12" "16" "20"
| That
|====================================== | 63%
| Something is fishy about our result! It appears that combining the | character vector with our matrix of numbers caused everything to be | enclosed in double quotes. This means we | character strings, which is no good.
...
|======================================= | 66%
| If you remember back to the beginning of this lesson, I told you | that matrices can only contain ONE class of data. Therefore, when | we tried to combine a character vector with a numeric matrix, R was | forced to | quotes.
...
|========================================= | 69%
| This is called | It just happened. But why didn | patients to numbers? I
...
|=========================================== | 71%
| So, we | of our patients in the table without destroying the integrity of | our numeric data. Try the following -- my_data <- | data.frame(patients, my_matrix)
> my_data<-data.frame(patients,my_matrix)
| You nailed it! Good job!
|============================================= | 74%
| Now view the contents of my_data to see what we
> my_data patients X1 X2 X3 X4 X5 1 Bill 1 5 9 13 17 2 Gina 2 6 10 14 18 3 Kelly 3 7 11 15 19 4 Sean 4 8 12 16 20
| Your dedication is inspiring!
|============================================== | 77%
| It looks like the data.frame() function allowed us to store our | character vector of names right alongside our matrix of numbers. | That
...
|================================================ | 80%
| Behind the scenes, the data.frame() function takes any number of | arguments and returns a single object of class `data.frame` that is | composed of the original objects.
...
|================================================== | 83%
| Let | created data frame.
> class(my_data) [1] "data.frame"
| That
|=================================================== | 86%
| It | columns of a data frame, which presents another possible way of | determining which row of values in our table belongs to each | patient.
...
|===================================================== | 89%
| However, since we | different problem by assigning names to the columns of our data | frame so that we know what type of measurement each column | represents.
...
|======================================================= | 91%
| Since we have six columns (including patient names), we | first create a vector containing one element for each column. | Create a character vector called cnames that contains the following | values (in order) -- "patient", "age", "weight", "bp", "rating", | "test".
> cnames<-c("patient","age","weight","bp","rating","test")
| Nice work!
|========================================================= | 94%
| Now, use the colnames() function to set the `colnames` attribute | for our data frame. This is similar to the way we used the dim() | function earlier in this lesson.
> colnames(my_data)<-cnames
| You are amazing!
|========================================================== | 97%
| Let
> my_data patient age weight bp rating test 1 Bill 1 5 9 13 17 2 Gina 2 6 10 14 18 3 Kelly 3 7 11 15 19 4 Sean 4 8 12 16 20
| You nailed it! Good job!
|============================================================| 100%
| In this lesson, you learned the basics of working with two very | important and common data structures -- matrices and data frames. | There | topics, particularly with respect to data frames, in future | lessons.
...
| Are you currently enrolled in the Coursera course associated with | this lesson?
1: Yes 2: No
Selection: 2
| You | menu...
|