Search code examples
rdataframepivotreshape

R function to mutate 2 ID columns into two different rows/entries


I have a data frame of events that have two columns for attendance IDs It looks like this.

event<-c(1:20)
name1<-c(101:120)
name2<-c(rep(NA,15),201:205)

df<-data.frame(event,name1,name2)

I want all the names in one column so I can join it to mult other data sets. It should look like this.

event<-c(1:16,16,17,17,18,18,19,19,20,20)
name<-c(101:116,201,117,202,118,203,119,204,120,205)

desired_df<-data.frame(event,name)

What is a good mutation/transformation to use for this process?


Solution

  • Using tidyr::pivot_longer():

    library(tidyr)
    
    desired_df <- df %>%
      pivot_longer(
        cols = c(name1, name2),
        values_drop_na = TRUE,
        names_to = NULL,
        values_to = "name")
    
    print(desired_df, n = Inf)
    # # A tibble: 25 × 2
    #    event  name
    #    <int> <int>
    #  1     1   101
    #  2     2   102
    #  3     3   103
    #  4     4   104
    #  5     5   105
    #  6     6   106
    #  7     7   107
    #  8     8   108
    #  9     9   109
    # 10    10   110
    # 11    11   111
    # 12    12   112
    # 13    13   113
    # 14    14   114
    # 15    15   115
    # 16    16   116
    # 17    16   201
    # 18    17   117
    # 19    17   202
    # 20    18   118
    # 21    18   203
    # 22    19   119
    # 23    19   204
    # 24    20   120
    # 25    20   205