Download mixedR.csv 

(see “CSV format” under “Files used in the examples”)

mixed_right <- read.csv('mixedR.csv')

mixed_right

  First Last Age   Type         A         B          C

1  John Sims  30 Veggie Artichoke  Broccoli     Carrot

2  Mary Sims  25 Veggie Asparagus               Celery

3  John Kent  40 Veggie                              

4  John Sims  30  Fruit     Apple    Banana     Cherry

5  Mary Sims  25  Fruit   Apricot            Cranberry

6  John Kent  40  Fruit   Avocado Blueberry Clementine

Option 1: Transform using reshape        

Option 2: Transform using reshape2 library        

Option 3: Transform using tidyr library        

Option 1: Transform using reshape

# reshape can only handle one time-varying variable at a time, so

# we need to use it twice, reshape the dataset to a temporary Long

# format first, and then reshape to Mixed Right format.

temp <- reshape(mixed_right,

                idvar = c('First','Last','Age','Type'),

                timevar = 'Group',

                times = c('A','B','C'),

                varying = c('A','B','C'),

                v.names = 'Plant',

                direction = 'long')

# rename the rows

row.names(temp)=1:nrow(temp)

temp

   First Last Age   Type Group      Plant
1   John Sims  30 Veggie     A  Artichoke
2   Mary Sims  25 Veggie     A  Asparagus
3   John Kent  40 Veggie     A          
4   John Sims  30  Fruit     A      Apple
5   Mary Sims  25  Fruit     A    Apricot
6   John Kent  40  Fruit     A    Avocado
7   John Sims  30 Veggie     B   Broccoli
8   Mary Sims  25 Veggie     B          
9   John Kent  40 Veggie     B          
10  John Sims  30  Fruit     B     Banana
11  Mary Sims  25  Fruit     B          
12  John Kent  40  Fruit     B  Blueberry
13  John Sims  30 Veggie     C     Carrot
14  Mary Sims  25 Veggie     C     Celery
15  John Kent  40 Veggie     C          
16  John Sims  30  Fruit     C     Cherry
17  Mary Sims  25  Fruit     C  Cranberry
18  John Kent  40  Fruit     C Clementine

mixed_left <- reshape(temp,

                       idvar = c('First','Last','Age','Group'),

                       timevar = 'Type',

                       v.names = 'Plant',

                       direction = 'wide')

names(mixed_left)

"First"        "Last"         "Age"          "Group"        

"Plant.Veggie" "Plant.Fruit"

# Fix the names removing the prefix from the data column names

names(mixed_left) <- gsub('Plant.','',names(mixed_left))

mixed_left

   First Last Age Group    Veggie      Fruit
1   John Sims  30     A Artichoke      Apple
2   Mary Sims  25     A Asparagus    Apricot
3   John Kent  40     A              Avocado
7   John Sims  30     B  Broccoli     Banana
8   Mary Sims  25     B                    
9   John Kent  40     B            Blueberry
13  John Sims  30     C    Carrot     Cherry
14  Mary Sims  25     C    Celery  Cranberry
15  John Kent  40     C           Clementine

Option 2: Transform using reshape2 library

library(reshape2)

temp <- melt(data = mixed_right,

             id.vars = c('First','Last','Age','Type'),

             measure.vars = c('A','B','C'),

             variable.name = 'Group',

             value.name = 'Plant')

temp

   First Last Age   Type Group      Plant

1   John Sims  30 Veggie     A  Artichoke

2   Mary Sims  25 Veggie     A  Asparagus

3   John Kent  40 Veggie     A          

4   John Sims  30  Fruit     A      Apple

5   Mary Sims  25  Fruit     A    Apricot

6   John Kent  40  Fruit     A    Avocado

7   John Sims  30 Veggie     B   Broccoli

8   Mary Sims  25 Veggie     B          

9   John Kent  40 Veggie     B          

10  John Sims  30  Fruit     B     Banana

11  Mary Sims  25  Fruit     B          

12  John Kent  40  Fruit     B  Blueberry

13  John Sims  30 Veggie     C     Carrot

14  Mary Sims  25 Veggie     C     Celery

15  John Kent  40 Veggie     C          

16  John Sims  30  Fruit     C     Cherry

17  Mary Sims  25  Fruit     C  Cranberry

18  John Kent  40  Fruit     C Clementine

mixed_left <- dcast(temp,

                     formula = First+Last+Age+Group~Type,

                     value.var = 'Plant')

mixed_left

  First Last Age Group      Fruit    Veggie

1  John Kent  40     A    Avocado          

2  John Kent  40     B  Blueberry          

3  John Kent  40     C Clementine          

4  John Sims  30     A      Apple Artichoke

5  John Sims  30     B     Banana  Broccoli

6  John Sims  30     C     Cherry    Carrot

7  Mary Sims  25     A    Apricot Asparagus

8  Mary Sims  25     B                    

9  Mary Sims  25     C  Cranberry    Celery

Option 3: Transform using tidyr library

library(tidyr)

mixed_left <- mixed_right %>%

  gather(key = 'Group', value = 'Plant', A:C) %>%

  spread(key= 'Type', value= 'Plant')

mixed_left

  First Last Age Group      Fruit    Veggie

1  John Kent  40     A    Avocado          

2  John Kent  40     B  Blueberry          

3  John Kent  40     C Clementine          

4  John Sims  30     A      Apple Artichoke

5  John Sims  30     B     Banana  Broccoli

6  John Sims  30     C     Cherry    Carrot

7  Mary Sims  25     A    Apricot Asparagus

8  Mary Sims  25     B                    

9  Mary Sims  25     C  Cranberry    Celery

Functions referenced:

read.csv

reshape, names, gsub, cbind, rownames, order, paste

reshape2: dcast, melt, colsplit

tidyr: gather, separate, unite, spread

More information:

RStudio Data Wrangling Cheatsheet

--- The End ---