Questions tagged [melt]

In R, the "melt" function from the "reshape2" and "data.table" (and earlier, "reshape") packages converts data into a long form. Melt is also a web application framework written in PHP.

In data processing, reshaping data to a long form where each row represents only one observation of one variable is often called "melting" the data, similar to UNPIVOT in some relational databases such as .

In , the function is part of the and packages (earlier, the "reshape" package).

This functionality is also found in similar data processing tools such as .

Related tags:

840 questions
17
votes
4 answers

Reshape wide format, to multi-column long format

I want to reshape a wide format dataset that has multiple tests which are measured at 3 time points: ID Test Year Fall Spring Winter 1 1 2008 15 16 19 1 1 2009 12 13 27 1 2 2008 22 22 …
Sam
  • 281
  • 4
  • 10
15
votes
1 answer

How to name each variable using melt

I have a matrix that I want to reform for plotting in ggplo2 using the melt function from reshape2 but cannot find a way to add custom header names. #Create toy data MyData <- matrix(rnorm(15,500), nrow = 5, ncol = 3, dimnames = list( …
Josh R.
  • 449
  • 2
  • 4
  • 13
13
votes
2 answers

How to change the melt.data.frame function in reshape2 package returned "variable" column to "character" class?

The default behavior of melt.data.frame is to return the "variable" column in "factor" class. Here is an example: > head(airquality) ozone solar.r wind temp month day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12…
rninja
  • 540
  • 1
  • 4
  • 12
11
votes
3 answers

Unmelt only part of a column from pandas dataframe

I have the following example dataframe: df = pd.DataFrame(data = {'RecordID' : [1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5], 'DisplayLabel' : ['Source','Test','Value 1','Value 2','Value3','Source','Test','Value 1','Value 2','Source','Test','Value…
Jon
  • 734
  • 1
  • 7
  • 30
11
votes
5 answers

Melting pandas data frame with multiple variable names and multiple value names

How can I melt a pandas data frame using multiple variable names and values? I have the following data frame that changes its shape in a for loop. In one of the for loop iterations, it looks like this: ID Cat Class_A Class_B Prob_A …
owise
  • 1,055
  • 16
  • 28
11
votes
2 answers

How can I add rows for all dates between two columns?

import pandas as pd mydata = [{'ID' : '10', 'Entry Date': '10/10/2016', 'Exit Date': '15/10/2016'}, {'ID' : '20', 'Entry Date': '10/10/2016', 'Exit Date': '18/10/2016'}] mydata2 = [{'ID': '10', 'Entry Date': '10/10/2016', 'Exit Date':…
Colin O'Brien
  • 2,175
  • 5
  • 20
  • 26
11
votes
1 answer

Using melt with matrix or data.frame gives different output

Consider the following code: set.seed(1) M = matrix(rnorm(9), ncol = 3) dimnames(M) = list(LETTERS[1:3], LETTERS[1:3]) print(M) A B C A -0.6264538 1.5952808 0.4874291 B 0.1836433 0.3295078 0.7383247 C -0.8356286…
Fernando
  • 7,785
  • 6
  • 49
  • 81
10
votes
1 answer

Name variables during multiple melt with data.table

I have the following data.table that I want to perform a multiple column melt upon: # Load library library(data.table) # Dummy data set dt <- data.table(a = 1:10, t_alpha = runif(10), t_beta = runif(10), …
Dan
  • 11,370
  • 4
  • 43
  • 68
10
votes
1 answer

Melt using patterns when variable names contain string information - avoid coercion to numeric

I am using the patterns() argument in data.table::melt() to melt data that has columns that have several easily-defined patterns. It is working, but I'm not seeing how I can create a character index variable instead of the default numeric…
Nancy
  • 3,989
  • 5
  • 31
  • 49
8
votes
3 answers

Python Pandas Sum Values in Columns If date between 2 dates

I have a dataframe df which can be created with this: data={'id':[1,1,1,1,2,2,2,2], 'date1':[datetime.date(2016,1,1),datetime.date(2016,1,2),datetime.date(2016,1,3),datetime.date(2016,1,4), …
clg4
  • 2,863
  • 6
  • 27
  • 32
8
votes
2 answers

How to reshape a dataframe with "reoccurring" columns?

I am new to data analysis with R. I recently got a pre-formatted environmental observation-model dataset, an example subset of which is shown below: date site obs mod site obs mod 2000-09-01 00:00:00 …
elarry
  • 521
  • 2
  • 7
  • 20
7
votes
5 answers

PySpark Dataframe melt columns into rows

As the subject describes, I have a PySpark Dataframe that I need to melt three columns into rows. Each column essentially represents a single fact in a category. The ultimate goal is to aggregate the data into a single total per category. There are…
Gary C
  • 93
  • 1
  • 1
  • 5
7
votes
1 answer

Melt datafarame with certain order in R

I am trying to melt this dataset: raw1 <- c("Alpha", "2018-04-01", "566.0", "647.75", "669.08", "673.08", "675.00") raw2 <- c("Alpha", "2018-04-02", "563.3", "651.08", "670.67", "684.58", "671.17") mydf <- as.data.frame(rbind(raw1,…
alvaropr
  • 699
  • 9
  • 20
7
votes
2 answers

R Violin plots and boxplots together, make fill behave differently only for boxplots

Ok, so I want to plot violin plots together with white boxplots, but my data is a little bit tricky. I melted data from data.frame with several columns, each of which has values corresponding to factor with two levels, here is approximation of my…
Poiu Rewq
  • 182
  • 13
7
votes
1 answer

In R, using melt(), how can I hide warning messages?

I'm melting some data and don't want to provide an id.var parameter to melt. The data melts fine, but I get the "No id variables; using all as measure variables" Is there a way to prevent that message from coming up, or a way to say…
Adam Price
  • 810
  • 2
  • 11
  • 21
1
2
3
55 56