0

I am working to melt some data using .

I have a series of tables (about 16), form The Office for National Statistics, merged into one spreadsheet. Each table covers a different type of measurement.

The first table is 16 industries over 6 years for 'Large Businesses.' Where the values are businesses. The second table is also 16 industry over 6 years for 'Medium Businesses.' Where the values are N businesses, and so forth.

I've had some luck Stacking one table using the below:

# install.packages("reshape2")
library(reshape2)

New_Test<- melt(Stack_Test, id.vars = c('Industry'), Var='TotBus' 'Year')
View(New_Test)

The issue is that the new stacked variables are named "Year" or "Totbus". The values are in the correct place, but the headings are not?

My desired output is a data frame with columns like this Industry, Year, Large business, Medium business, ect. and with the values below in columns.

How can I do this? Is there a resource I can follow along with to learn this slightly better?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
El_Commi
  • 1
  • 2
  • 2
    I'm not sure I understand what you are trying to do. Could you provide an example of the type of data you are trying to stack, and what a properly stacked solution would look like? – Brandon Feb 10 '18 at 00:17
  • 2
    There's a typo in the code above where you need 'TotBus' and 'Year' to be in a vector (wrap with c()). But that line of code wouldn't have executed if you entered it as you have it above. Also, you're more likely to get help on this if you can provide a small amount of data you're working with in the code above (it can be fake). – cmaimone Feb 10 '18 at 01:10
  • I second both comment about. It's more likely that we will be able to help you if you provide [a complete minimal reproducible example](http://stackoverflow.com/help/mcve) to go along with your question. In addtion, you ask for resource to follow, tutorials, I will recommend taking a look at [these examples](http://tidyr.tidyverse.org/reference/gather.html) using `gather` from the `tidyr` package in the [tag:tidyverse] universe. It is like the `melt` from [tag:reshape2], but I think it’s more transparent in the code. – Eric Fail Feb 10 '18 at 11:19
  • Thanks for the responses! Most of data I'm using is publicly available, but I couldn't work out how to add a table here. So I've uploaded 2 pictures I hope that's ok. The first is how I have the data currently. There are basically a series of tables (About 15 or so) that look like this. https://ibb.co/c9u1mS I want to stack them so it looks like this. https://ibb.co/nju1mS After that, I'll be merging a few data sets to start the analysis. Previously for the others I copy and pasted. But it's time consuming (and I have a lot more files to go..) – El_Commi Feb 13 '18 at 13:01
  • I accidentally cut off the year column in the stacked example. It's to the left of the industry colum. THe data can be found on the ONS Nomis websit here: https://www.nomisweb.co.uk/query/construct/summary.asp?reset=yes&mode=construct&dataset=142&version=0&anal=1&initsel= Thanks again! – El_Commi Feb 13 '18 at 13:13

0 Answers0