Erik Rehnberg Steeb
Erik Rehnberg Steeb

Reputation: 57

Duplicate rows in R based on content of columns

I'm working with a school schedule dataset for a visualization project and had days of classes originally in the form "MW" or "TTh" etc - they are now in the format below:

name      start   end    first   second
finance   9:00    10:00  M       W
stats     10:30   11:30  T       Th
econ      16:30   19:00  T       NA

I'm looking to duplicate the first three columns to get a dataframe that looks like:

day  name      start   end   
M    finance   9:00    10:00 
W    finance   9:00    10:00
T    stats     10:30   11:30
Th   stats     10:30   11:30
W    econ      10:30   11:30

Any ideas?

Upvotes: 1

Views: 41

Answers (1)

akrun
akrun

Reputation: 887048

We can use pivot_longer

library(dplyr)
library(tidyr)
pivot_longer(df1, cols = c(first, second), values_to = 'day', 
        names_to = 'name1') %>%
   select(day, name, start, end) %>%
   filter(complete.cases(day))

-output

# A tibble: 5 x 4
#  day   name    start end  
#  <chr> <chr>   <chr> <chr>
#1 M     finance 9:00  10:00
#2 W     finance 9:00  10:00
#3 T     stats   10:30 11:30
#4 Th    stats   10:30 11:30
#5 T     econ    16:30 19:00

data

df1 <- structure(list(name = c("finance", "stats", "econ"), start = c("9:00", 
"10:30", "16:30"), end = c("10:00", "11:30", "19:00"), first = c("M", 
"T", "T"), second = c("W", "Th", NA)), class = "data.frame", row.names = c(NA, 
-3L))

Upvotes: 3

Related Questions