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
5
votes
1 answer

retaining names of levels of variables while melting

Is there any way to retain names of the original levels of variables that are being melted? For example, in the example below, is there any way to get "alpha", "beta", and "gamma" instead of "1", "2", "3". I can, of course, rename them but the…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
5
votes
3 answers

Pandas - melt, stack, reshape or MultiIndex dataframe columns based on multiple attributes from the column name

I have a Pandas dataframe with data in a very wide form... for example: ID Equipment Function Task exprt_cond1_time exprt_cond2_time exprt_cond1_freq exprt_cond2_freq novce_cond1_time novce_cond2_time novce_cond1_freq …
CJH
  • 1,295
  • 1
  • 11
  • 15
5
votes
1 answer

Merge several data frames on two common columns

I've seen some questions about merging csv files into one data frame. What if the data frames are already in the workspace. I have five wide zoos that I cast as data frames, then melt. Here's the head of one: > head(df.mon.ssf.ret) date…
Richard Herron
  • 9,760
  • 12
  • 69
  • 116
5
votes
2 answers

Melt data frame row by row

How can I melt a data frame row by row? I found a really similar question on the forum but I still can't solve my problem without a different id variable. This is my data set: V1 V2 V3 V4 V5 51 20 29 12 20 51 22 51 NA NA 51 14 NA NA NA 51 75 NA NA…
tiffkyn
  • 79
  • 1
  • 7
5
votes
2 answers

How to specify level names when using reshape2::melt on a list?

Question I find myself using reshape2::melt to obtain a single "long" data.frame from a hierarchical list of data.frame objects. However, the column names of the result has the list hierarchy levels labeled as "L1", "L2", etc. However, since those…
Kalin
  • 1,691
  • 2
  • 16
  • 22
5
votes
1 answer

R tcrossprod with unique combinations

There might be a version of tcrossprod that achieves this but I wasn't able to find it. From the example below, how to I get only rows with the first occurrence of a combination, if order doesn't matter? I.e. (1,2) for me is the same as (2,1). a <-…
ADJ
  • 4,892
  • 10
  • 50
  • 83
5
votes
2 answers

Convert data frame from wide to long with 2 variables

I have the following wide data frame (mydf.wide): DAY JAN F1 FEB F2 MAR F3 APR F4 MAY F5 JUN F6 JUL F7 AUG F8 SEP F9 OCT F10 NOV F11 DEC F12 1 169 0 296 0 1095 0 599 0 1361 0 1746 0 2411 0 2516 0 1614 0 …
user2165907
  • 1,401
  • 3
  • 13
  • 28
5
votes
1 answer

Reshape data.frame with two columns into multiple columns with data (R)

A trivial question but I cant find the answer as of yet. I want to split the dataframe column 'year' into a set of new columns with each year the column name and subsequent data below it: Year FQ 1975 3.156 1975 8.980 1977 10.304 …
Salmo salar
  • 517
  • 1
  • 5
  • 17
4
votes
3 answers

How to use pd.melt to unpivot a dataframe where columns share a prefix?

I'm trying to unpivot my data using pd.melt but no success so far. Each row is a business, and the data contains information about the business and multiple reviews. I want my data to have every review as a row. My first 150 columns are in groups of…
raz
  • 43
  • 4
4
votes
2 answers

How to 'reverse melt' a data.frame?

I have data.frame df1 (see code below). I would like to convert it to what df2 looks like (see code below). Maybe this can be done with reshape cast or reverse melt? But I do not understand these functions. Can anyone help, please? df1 <-…
Sylvia Rodriguez
  • 1,203
  • 2
  • 11
  • 30
4
votes
3 answers

Using melt / cast with variables of uneven length in R

I'm working with a large data frame that I want to pivot, so that variables in a column become rows across the top. I've found the reshape package very useful in such cases, except that the cast function defaults to fun.aggregate=length. Presumably…
Jon
  • 41
  • 1
  • 2
4
votes
2 answers

Pandas pivot? pivot_table? melt? stack or unstack?

I have a dataframe that looks like this: id Revenue Cost qty time 0 A 400 50 2 1 1 A 900 200 8 2 2 A 800 100 8 3 3 B 300 20 1 1 4 B 600 …
user3486773
  • 1,174
  • 3
  • 25
  • 50
4
votes
3 answers

Long pivot for multiple variables using Pivot long

I am trying to reshape wide table data to long using Pivot_longer from Tidyr. However, not able to achieve the result - tried searching but could not find the exact scenario. Example: x<- read.table(header=T, text=' Dt1 V1_cur V2_cur…
user2533159
  • 43
  • 1
  • 3
4
votes
2 answers

How to melt `pandas.DataFrame` with hour columns and divide them on 15 minutes intervals

I have a DataFrame something like this: data = [['2019-01-01', .1, .2], ['2019-01-02', .5, .3], ['2019-01-03', .2, .4]] df = pd.DataFrame(data, columns=['date', 'hour01', 'hour02']) date hour01 hour02 0 2019-01-01 …
miller
  • 1,636
  • 3
  • 26
  • 55
4
votes
3 answers

Wide to long returns empty output - Python dataframe

I have a dataframe which can be generated from the code as given below df = pd.DataFrame({'person_id' :[1,2,3],'date1': ['12/31/2007','11/25/2009','10/06/2005'],'val1': [2,4,6],'date2':…
The Great
  • 7,215
  • 7
  • 40
  • 128