Download long.csv (see “CSV format” under “Files used in the examples”) long <- read.csv('long.csv') long First Last Age Group Type Plant 1 John Sims 30 A Veggie Artichoke 2 Mary Sims 25 A Veggie Asparagus 3 John Kent 40 A Veggie 4 John Sims 30 B Veggie Broccoli 5 Mary Sims 25 B Veggie 6 John Kent 40 B Veggie 7 John Sims 30 C Veggie Carrot 8 Mary Sims 25 C Veggie Celery 9 John Kent 40 C Veggie 10 John Sims 30 A Fruit Apple 11 Mary Sims 25 A Fruit Apricot 12 John Kent 40 A Fruit Avocado 13 John Sims 30 B Fruit Banana 14 Mary Sims 25 B Fruit 15 John Kent 40 B Fruit Blueberry 16 John Sims 30 C Fruit Cherry 17 Mary Sims 25 C Fruit Cranberry 18 John Kent 40 C Fruit Clementine Option 1: Transform using reshape Option 2: Transform using reshape2 library Option 3: Transform using tidyr library Option 1: Transform using reshape |
mixed_right <- reshape(long, idvar = c('First','Last','Age','Type'), timevar = 'Group', v.names = 'Plant', direction = 'wide') #Fix the names removing the prefix from the data column names names(mixed_right) <- gsub('Plant.','',names(mixed_right)) 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 10 John Sims 30 Fruit Apple Banana Cherry 11 Mary Sims 25 Fruit Apricot Cranberry 12 John Kent 40 Fruit Avocado Blueberry Clementine |
library(reshape2) mixed_right = dcast(data = long, formula = First + Last + Age + Type ~ Group, value.var = 'Plant', drop = TRUE) mixed_right First Last Age Type A B C 1 John Kent 40 Fruit Avocado Blueberry Clementine 2 John Kent 40 Veggie 3 John Sims 30 Fruit Apple Banana Cherry 4 John Sims 30 Veggie Artichoke Broccoli Carrot 5 Mary Sims 25 Fruit Apricot Cranberry 6 Mary Sims 25 Veggie Asparagus Celery |
library(tidyr) mixed_right <- long %>% spread(key = 'Group', value = 'Plant') #Optional: sort data for easier comparison library(plyr) arrange(mixed_right, Type, A) First Last Age Type A B C 1 John Sims 30 Fruit Apple Banana Cherry 2 Mary Sims 25 Fruit Apricot Cranberry 3 John Kent 40 Fruit Avocado Blueberry Clementine 4 John Kent 40 Veggie 5 John Sims 30 Veggie Artichoke Broccoli Carrot 6 Mary Sims 25 Veggie Asparagus Celery |
Functions referenced:
reshape, names, gsub, cbind, rownames, order, paste
reshape2: dcast, melt, colsplit
tidyr: gather, separate, unite, spread
More information:
RStudio Data Wrangling Cheatsheet
--- The End ---