wide to long format

Hi everyone.
My data looks like Table 1. I want to convert it into Table 2.
Not sure how to do it.

I appreciate any help.

Thank you.

Table 1

Code Age Languages Response
A 24 001, 013 1
B 18 025 0
C 27 008, 009, 010 1

Table 2

Code Age Languages Response
A 24 001 1
A 24 003 1
B 18 025 0
C 27 008 1
C 27 009 1
C 27 010 1

Here is how to do it

library(tidyverse)

# Sample data in a copy/paste friendly format
table_1 <- data.frame(
  stringsAsFactors = FALSE,
              Code = c("A", "B", "C"),
               Age = c(24, 18, 27),
         Languages = c("001, 013", "025", "008, 009, 010"),
          Response = c(1, 0, 1)
)

# Relevant code
table_1 %>% 
    separate_rows(Languages, sep = ",\\s")
#> # A tibble: 6 × 4
#>   Code    Age Languages Response
#>   <chr> <dbl> <chr>        <dbl>
#> 1 A        24 001              1
#> 2 A        24 013              1
#> 3 B        18 025              0
#> 4 C        27 008              1
#> 5 C        27 009              1
#> 6 C        27 010              1

Created on 2021-12-12 by the reprex package (v2.0.1)

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.