-1

so I'm new to coding and need some help with data cleaning/organizing. My data currently looks like this:

Date                  Case Num                  Name
12/16                  1, 2                     Smith, John
12/16                  3, 4                     Smith, John
01/17                  5, 6                     Smith, John

And I want it to look like this:

Date                  Case Num                  Name
12/16                  1                        Smith, John
12/16                  2                        Smith, John
12/16                  3                        Smith, John
12/16                  4                        Smith, John
01/17                  5                        Smith, John
01/17                  6                        Smith, John

Please help. Thank you!

camille
  • 16,432
  • 18
  • 38
  • 60
Anna Rouw
  • 69
  • 2
  • 8

2 Answers2

2
library(tidyverse)
dat%>%
  mutate(Case.Num=strsplit(Case.Num,","))%>%
  unnest()
   Date        Name Case.Num
1 12/16 Smith, John        1
2 12/16 Smith, John        2
3 12/16 Smith, John        3
4 12/16 Smith, John        4
5 01/17 Smith, John        5
6 01/17 Smith, John        6
Onyambu
  • 67,392
  • 3
  • 24
  • 53
1

You can use separate_rows from the tidyr package

library(tidyr)
df1 %>% separate_rows(Case_Num)

   Date Case_Num        Name
1 12/16        1 Smith, John
2 12/16        2 Smith, John
3 12/16        3 Smith, John
4 12/16        4 Smith, John
5 01/17        5 Smith, John
6 01/17        6 Smith, John

data:

df1 <- structure(list(Date = c("12/16", "12/16", "01/17"), 
                      Case_Num = c("1, 2","3, 4", "5, 6"), 
                      Name = c("Smith, John", "Smith, John", "Smith, John")), 
                 class = "data.frame", 
                 row.names = c(NA, -3L))
phiver
  • 23,048
  • 14
  • 44
  • 56