Converting from wide to long

Choice Modeling for Marketing in R

Elea McDonnell Feit

Assistant Professor of Marketing, Drexel University

Why long format?

Long choice data

Choice Modeling for Marketing in R

Sportscar data in wide format

head(sportscar_wide)
  resp_id ques segment choice seat.1 seat.2 seat.3 trans.1 trans.2 trans.3
1       1    1   basic      3      2      5      5  manual    auto    auto
2       1    2   basic      2      5      2      4  manual  manual    auto
3       1    3   basic      1      5      4      4    auto    auto  manual
4       1    4   basic      3      2      4      4  manual  manual    auto
5       1    5   basic      2      5      5      2  manual  manual    auto
6       1    6   basic      3      2      4      2    auto  manual    auto
  convert.1 convert.2 convert.3 price.1 price.2 price.3
1       yes        no        no      35      40      30
2        no        no        no      35      30      35
3       yes       yes        no      35      30      40
4       yes       yes       yes      30      40      35
5       yes        no       yes      40      30      40
6       yes       yes        no      35      35      30
Choice Modeling for Marketing in R

Transforming from wide to long

sportscar <- reshape(
  sportscar_wide, 
  direction = "long", 
  varying = list(seat = 5:7, trans = 8:10, convert = 11:13, price = 14:16), 
  v.names = c("seat", "trans", "convert", "price"), 
  timevar = "alt")

head(sportscar)
    resp_id ques segment choice alt seat  trans convert price id
1.1       1    1   basic      3   1    2 manual     yes    35  1
2.1       1    2   basic      2   1    5 manual      no    35  2
3.1       1    3   basic      1   1    5   auto     yes    35  3
4.1       1    4   basic      3   1    2 manual     yes    30  4
5.1       1    5   basic      2   1    5 manual     yes    40  5
6.1       1    6   basic      3   1    2   auto     yes    35  6
Choice Modeling for Marketing in R

Sorting the long data

new_order <- order(sportscar$resp_id, 
                   sportscar$ques, 
                   sportscar$alt)
sportscar <- sportscar[new_order,]

head(sportscar)
    resp_id ques segment choice alt seat  trans convert price id
1.1       1    1   basic      3   1    2 manual     yes    35  1
1.2       1    1   basic      3   2    5   auto      no    40  1
1.3       1    1   basic      3   3    5   auto      no    30  1
2.1       1    2   basic      1   1    5 manual      no    35  2
2.2       1    2   basic      1   2    2 manual      no    30  2
2.3       1    2   basic      1   3    4   auto      no    35  2
Choice Modeling for Marketing in R

Converting choice to a logical

sportscar$choice <- sportscar$choice == sportscar$alt

head(sportscar)
    resp_id ques segment choice alt seat  trans convert price id
1.1       1    1   basic  FALSE   1    2 manual     yes    35  1
1.2       1    1   basic  FALSE   2    5   auto      no    40  1
1.3       1    1   basic   TRUE   3    5   auto      no    30  1
2.1       1    2   basic   TRUE   1    5 manual      no    35  2
2.2       1    2   basic  FALSE   2    2 manual      no    30  2
2.3       1    2   basic  FALSE   3    4   auto      no    35  2
Choice Modeling for Marketing in R

Let's practice!

Choice Modeling for Marketing in R

Preparing Video For Download...