How to Use Gather Function in R?-tidyr Part2

How to Use Gather Function in R?, To “collect” a key-value pair across many columns, use the gather() function from the tidyr package.

How to Use Spread Function in R?-tidyr Part1

The basic syntax used by this function is as follows.

gather(data, key value, …)

where:

data: Name of the data frame

key: Name of the key column to create

value: Name of the newly created value column

: Choose which columns to collect data from.

How to Use Gather Function in R

The practical application of this function is demonstrated in the examples that follow.

dplyr Techniques and Tips – Data Science Tutorials

Example 1: Gather Values From Two Columns

Let’s say we have the R data frame shown below:

make a data frame

df <- data.frame(player=c('P1', 'P2', 'P3', 'P4'),
year1=c(212, 215, 319, 129),
year2=c(232, 229, 158, 122))

Let’s view the data frame

df
  player year1 year2
1     P1   212   232
2     P2   215   229
3     P3   319   158
4     P4   129   122

The gather() function can be used to add two new columns named “year” and “points,” respectively:

library(tidyr)

get information from columns 2 and 3

gather(df, key="year", value="points", 2:3)
   player  year points
1     P1 year1    212
2     P2 year1    215
3     P3 year1    319
4     P4 year1    129
5     P1 year2    232
6     P2 year2    229
7     P3 year2    158
8     P4 year2    122

Example 2: Collect Information From More Than Two Columns

Let’s say we have the R data frame shown below:

Let’s create a data frame

df2 <- data.frame(player=c('P1', 'P2', 'P3', 'P4'),
                  year1=c(213, 112, 112, 114),
                  year2=c(122, 229, 215, 211),
                  year3=c(123, 122, 122, 129))

Now we can view the data frame

df2
  player year1 year2 year3
1     P1   213   122   123
2     P2   112   229   122
3     P3   112   215   122
4     P4   114   211   129

The data from columns 2, 3, and 4 can be “gathered” into two additional columns called “year” and “points” using the gather() method as follows:

library(tidyr)

assemble information from columns 2, 3, and 4.

gather(df2, key="year", value="points", 2:4)
    player  year points
1      P1 year1    213
2      P2 year1    112
3      P3 year1    112
4      P4 year1    114
5      P1 year2    122
6      P2 year2    229
7      P3 year2    215
8      P4 year2    211
9      P1 year3    123
10     P2 year3    122
11     P3 year3    122
12     P4 year3    129

Best Books to learn Tensorflow – Data Science Tutorials

Have you liked this article? If you could email it to a friend or share it on Facebook, Twitter, or Linked In, I would be eternally grateful.

Please use the like buttons below to show your support. Please remember to share and comment below. 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

three × one =