Is it possible to use spread on multiple columns in tidyr similar to dcast? [duplicate]

One option would be to create a new ‘Prod_Count’ by joining the ‘Product’ and ‘Country’ columns by paste, remove those columns with the select and reshape from ‘long’ to ‘wide’ using spread from tidyr. library(dplyr) library(tidyr) sdt %>% mutate(Prod_Count=paste(Product, Country, sep=”_”)) %>% select(-Product, -Country)%>% spread(Prod_Count, value)%>% head(2) # Year A_AI B_EI #1 1990 0.7878674 0.2486044 … Read more

melt / reshape in excel using VBA?

I’ve got two posts, with usable code and downloadable workbook, on doing this in Excel/VBA on my blog: http://yoursumbuddy.com/data-normalizer http://yoursumbuddy.com/data-normalizer-the-sql/ Here’s the code: ‘Arguments ‘List: The range to be normalized. ‘RepeatingColsCount: The number of columns, starting with the leftmost, ‘ whose headings remain the same. ‘NormalizedColHeader: The column header for the rolled-up category. ‘DataColHeader: The … Read more

Grouped bar plot in ggplot

EDIT: Many years later For a pure ggplot2 + utils::stack() solution, see the answer by @markus! A somewhat verbose tidyverse solution, with all non-base packages explicitly stated so that you know where each function comes from: library(magrittr) # needed for %>% if dplyr is not attached “http://pastebin.com/raw.php?i=L8cEKcxS” %>% utils::read.csv(sep = “,”) %>% tidyr::pivot_longer(cols = c(Food, … Read more

Reshape multiple values at once

In “reshape2”, you can use recast (though in my experience, this isn’t a widely known function). library(reshape2) recast(mydf, id ~ variable + type, id.var = c(“id”, “type”)) # id transactions_expense transactions_income amount_expense amount_income # 1 20 25 20 95 100 # 2 30 45 50 250 300 You can also use base R’s reshape: reshape(mydf, … Read more

Reshaping multiple sets of measurement columns (wide format) into single columns (long format)

reshape(dat, idvar=”ID”, direction=”long”, varying=list(Start=c(2,5,8), End=c(3,6,9), Value=c(4,7,10)), v.names = c(“DateRangeStart”, “DateRangeEnd”, “Value”) ) #————- ID time DateRangeStart DateRangeEnd Value 1.1 1 1 1/1/90 3/1/90 4.4 1.2 1 2 4/5/91 6/7/91 6.2 1.3 1 3 5/5/95 6/6/96 3.3 (Added the v.names per Josh’s suggestion.)