Data Frames in R: Part III

In this post, we continue our look at using data frames in R. Specifically, we will look at at extracting values from a data frame, adding observations, and adding variables to a data frame.

Before we begin, you need to setup the following dataframe in Rstudio.  Look for the code for ‘points.team’.

Extracting Values

Extracting values in a data frame involves the same steps as extracting values in a matrix. For example, let’s say you want to know how many points ‘Kevin’ scored in game three. Below is one way to find out.

> points.team['3rd', 'Kevin']
[1] 25

There little to explain here. Kevin scored 25 points in game 3.

Another important argument to know is the dollar sign “$”. This argument is used to extract a variable from a data frame, matrix, or array. For example, let’s say you want to know for each game how many points ‘James’ scored. Below is how to do this.

> points.team$James
[1] 12 15 30 25 23 32

By using the dollar sign “$” you are able to only see the results for ‘James’.

Adding Observations

It is also possible to add observations to data frames. For example, if you wanted to add the results of a 7th game to the ‘points.team’ data frame you would do the following.

> points.team
    James Kevin
1st    12    20
2nd    15    19
3rd    30    25
4th    25    30
5th    23    31
6th    32    22
> points.team<- rbind(points.team, '7th' = c(17,14))
> points.team
    James Kevin
1st    12    20
2nd    15    19
3rd    30    25
4th    25    30
5th    23    31
6th    32    22
7th    17    14

Here is what happen

  1. We displayed ‘points.team’ as a reference point.
  2. We then assigned the variable ‘points.team’ the following additional information using the ‘rbind’ function
    1. The data frame ‘points.team’
    2. A row named ‘7th’
    3. Two values one ’17’ and the other ’14’
  3. We then print ‘points.team’ again so you can see the difference

In the seventh game ‘James’ scored 17 while ‘Kevin’ scored 14.

You can add multiple rows by doing the following

> points.team
    James Kevin
1st    12    20
2nd    15    19
3rd    30    25
4th    25    30
5th    23    31
6th    32    22
7th    17    14
> more.points<-data.frame(James=c(25,35), Kevin=c(27,29))
> rownames(more.points)<- c('8th','9th')
> points.team<-rbind(points.team, more.points)
> points.team
    James Kevin
1st    12    20
2nd    15    19
3rd    30    25
4th    25    30
5th    23    31
6th    32    22
7th    17    14
8th    25    27
9th    35    29

Here is what happened

  1. Displayed ‘points.team’ for comparison
  2. Created the data frame ‘more.points’. This is the data frame we are going to add to ‘points.team’
  3. We name the rows in ‘more.points’ ‘8th’ and ‘9th’
  4. We bind ‘more.points’ to ‘points.team’ using the ‘rbind’ function
  5. We then print the results of ‘points.team’ for comparison.

Adding Variables

You can also add additional variables to a data frame. For example, let’s say we want to add the results of a player named “Mo”. Below is how to do this.

> points.of.Mo<-c(10,8,9,15,17,12,16,20,13)
> points.team$Mo<-points.of.Mo
> points.team
    James Kevin Mo
1st    12    20 10
2nd    15    19  8
3rd    30    25  9
4th    25    30 15
5th    23    31 17
6th    32    22 12
7th    17    14 16
8th    25    27 20
9th    35    29 13

So what happened?

We created the variable ‘points.of.Mo’. Next, used the ‘points.team’ variable with the $ to add a variable named ‘Mo’. We assigned to this variable the ‘points.of.Mo’. Finally we printed the ‘points.team’ data frame and you can see the new column ‘Mo’.

Conclusion

This completes are discussion on the basics of data frames. There are many more concepts that could be discussed but the goal is not to be exhaustive. The true goal is to assist people who are new to this programming

Advertisements

2 thoughts on “Data Frames in R: Part III

  1. Pingback: Data Frames in R: Part III | Education and Rese...

  2. Pingback: Using ‘for’ Loops in R | educationalresearchtechniques

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s