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
1
vote
2 answers

How to further melt a dataset?

I'm working with the following data: #Reproducible Example Bills <- c("93-HCONRES-106", "93-HCONRES-107", "93-HCONRES-108") Members <- c("00134", "00416;00010;00017;00026", "00416;00503;00513;00568") data <- data.frame(Bills, Members) Where the…
Sharif Amlani
  • 1,138
  • 1
  • 11
  • 25
1
vote
3 answers

R group repeating values

If I am dealing with a dataset like this Id Index Value 1233 i1 Blue 1233 i2 Blue 1233 i3 Blue 6545 i1 Red 6545 i2 NA 6545 i3 Black 4177 i1 NA 4177 i2 NA …
Kim Jenkins
  • 438
  • 3
  • 17
1
vote
1 answer

Optimize Melt In Python

Input df: ID Date Start End ABC 2012 15 17 ABC 2013 11 20 XYZ 2012 30 50 XYZ 2014 50 100 Expected Output outputdf: ID Date Variable Value ABC 2012 Start 15 ABC 2012 …
Jain
  • 959
  • 2
  • 13
  • 31
1
vote
1 answer

Pivot dataframe by column (ID) duplicated

I have a DataFrame with a column named 'ID' that has duplicate observations. Each 'ID' row has one or more 'Article' values columns. I want to transpose the whole dataframe grouping by 'ID' adding new columns at the same row of a unique 'ID'. Order…
jabeono
  • 117
  • 5
1
vote
2 answers

pair wise melt in pandas dataframe

I want to pair wise create a new dataframe. I tried using MELT but it did not really work. If you notice columns are pair wise ( i.e code:type , code1:type1 ) I tried creating a list of code columns and list of type columns and then do melt. How…
Sai Astro
  • 113
  • 1
  • 7
1
vote
1 answer

reshaping and melting dataframe whilst picking up certain regex

I have a dataframe that looks like: Location Account Y2019:MTD:January:Expense Y2019:MTD:January:Income Y2019:MTD:February:Expense Madrid ABC 4354 56456 235423 Madrid XYX …
asimo
  • 2,340
  • 11
  • 29
1
vote
2 answers

Melt data frame with multiple identically named 'id.vars'

I have a data frame with multiple identically named columns: > df <- data.frame(X1=c(1:4),Y1=c(16:13),X2=c(4:7),Y2=c(-1:-4),X3=c(3:6),Y3=c(2:-1)) > df X1 Y1 X2 Y2 X3 Y3 1 1 16 4 -1 3 2 2 2 15 5 -2 4 1 3 3 14 6 -3 5 0 4 4 13 7 -4 6…
Jdh
  • 91
  • 5
1
vote
1 answer

Reshape a dataframe for all values in a column

I have a dataframe like so: number type X Y Z 1 red 101 NaN 101,NaN 2 blue 101 40,50 101,40,50 3 green 401 70,NaN 101,70,NaN Is there any way that…
Mazz
  • 770
  • 3
  • 11
  • 23
1
vote
1 answer

R: How to create new variable based on name of other column

I would like to create a new variable based on the name of other columns, so that I can move from a wide to a long dataset. What I have: df <- data.frame(name = c("1", "2", "3", "4"), alpha_1 = c(50, 30, 20, 6), …
JBL
  • 37
  • 2
1
vote
1 answer

Rearrange GIS data of Origina Destinations to one row each point

I start with a "wide" data set of GIS data of Origin Destinations (OD), that I want to rearange into a longer data set of one row for each point. I already managed to melt and then dcast to rearange the first layer of the ODs, but I am strungling…
Max M
  • 806
  • 14
  • 29
1
vote
2 answers

How to transform multi rows columns and melt to long form R data.table

There is the table to transform into long form from wide form. It contains +200 columns compose with multi columns like below : Original data : # dt dt <- data.table("1" = c(NA,"Place","dan","uan","yan"), "2" =…
rane
  • 901
  • 4
  • 12
  • 24
1
vote
1 answer

Python pandas melt single column into two seperate

I have pandas that looks at follows: df = id A_value D_value 1 50 60 2 33 45 And I want to split it to: df = id value value_type 1 50 A 1 60…
Cranjis
  • 1,590
  • 8
  • 31
  • 64
1
vote
1 answer

Melting two sets of two columns into two rows (one row for each column in the set)

I have a data.table as follows: DT <- fread( "ID country year Event_A Event_B Event_A_succ Event_B_succ 4 NLD 2002 0 1 0 0 5 NLD 2002 0 1 0 1 6 NLD 2006 1 1 1 1 7 NLD 2006 1 0 1 0 8 NLD 2006 1 1 0…
Tom
  • 2,173
  • 1
  • 17
  • 44
1
vote
1 answer

Melting/Splitting a row into two rows, using two column values in the original row, leaving the rest intact

I have a data.table as follows: DT <- fread( "ID country year Event_A Event_B 4 NLD 2002 0 1 5 NLD 2002 0 1 6 NLD 2006 1 1 7 NLD 2006 1 0 8 NLD 2006 1 1 9 GBR 2002 0 1 10 GBR 2002 0 0 11 GBR 2002 0…
Tom
  • 2,173
  • 1
  • 17
  • 44
1
vote
1 answer

How do I pass multiple column names as val_vars using melt?

I have a large data frame (367 rows × 342 columns) where multiple columns have the same prefix in their name. I am trying to make our code easier to use. Current code: value_vars = "'Intensity 01_1', 'Intensity 01_2', …