So far we have looked at vectors, matrices, and arrays. One thing these three objects have in common is that they consist of one type of data. In other words, vectors, matrices and arrays contain either numerical or character information but not both at the same time.
Data frames are different. They allow you to have a mixture of information all contained within one place. You can have character data, such as names, and numerical data, such as salaries all in one place. In this post, we will first look at how to convert a matrix to a data frame.
Converting a Matrix to a Data Frame
One of the benefits of converting a matrix to a data frame is that the columns in a matrix become variables in a data frame. This is useful for data analysis at times. In the example below, we will convert the matrix of ‘points.team’ into a data frame. In order to do this we have to use to new functions the ‘as.data.frame’ function which converts the matrix into a data frame and the ‘t’ function which transposes the rows so that they become the columns. Below is the code for completing this
> points.team 1st 2nd 3rd 4th 5th 6th James 12 15 30 25 23 32 Kevin 20 19 25 30 31 22 > team.points.df <- as.data.frame(t(points.team)) > team.points.df James Kevin 1st 12 20 2nd 15 19 3rd 30 25 4th 25 30 5th 23 31 6th 32 22
This is what we did
- We displayed the matrix ‘points.team’ as a reference. The code for creating this is available here.
- We then created the variable ‘team.points.df’ and used two functions to convert the matrix ‘points.team’ to a data frame
- ‘as.data.frame’ was used to make the actually data frame
- ‘t’ was used to move or transpose the names of the rows in the matrix (James and Kevin) to be the names of columns in the data frame. This gives us two variables (James and Kevin) with six entries (1st-6th) if we had not done this we would have made the example below
> team.points.df 1st 2nd 3rd 4th 5th 6th James 12 15 30 25 23 32 Kevin 20 19 25 30 31 22
In this example, we did not transpose ‘James’ and ‘Kevin’ to be columns. Instead 1st-6th are the variables instead of ‘James’ and ‘Kevin’. For our purposes this does not make sense but it may be appropriate it other situations.
- The last step involved displaying the new data frame ‘team.points.df’
Using the ‘str’ function allows you to learn some information about a data frame as in the example below
> str(team.points.df) 'data.frame': 6 obs. of 2 variables: $ James: num 12 15 30 25 23 32 $ Kevin: num 20 19 25 30 31 22
Here is what we now know about our data frame
- The variable is a data frame (data.frame)
- It has six observations (6 obs.) which are the points scored by the players in six games
- There are two variables ‘James’ and ‘Kevin’
- The variables are numeric (num)
This is just the beginning of our examination of data frames in R. In a future post we will look at making original data frames.