Site icon R-bloggers

How to Use Gather Function in R?-tidyr Part2

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to Use Gather Function in R?-tidyr Part2 appeared first on Data Science Tutorials

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

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

Check your inbox or spam folder to confirm your subscription.

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. 

The post How to Use Gather Function in R?-tidyr Part2 appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.