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

Un-melting a dataframe in generalised way

Let say I have below data.frame library(reshape2) set.seed(1) dat = data.frame(X1 = sample(letters, 10, replace = T), X2 = sample(letters, 10, replace = T), X3 = sample(LETTERS, 10, replace = T), X4 = sample(LETTERS[1:4], 10, replace = T), X5 =…
Brian Smith
  • 1,200
  • 4
  • 16
1
vote
1 answer

Using melt only for certain columns in R

Below is the sample data. Pct10,pct25,median,and pct75 are hourly wages. Their counterparts below are annual salaries. The quandary is how to basically fold only these below while duplicating the descriptive items; industry and occupation. The…
Tim Wilcox
  • 1,275
  • 2
  • 19
  • 43
1
vote
1 answer

How to melt multindex row and maintain the index information in the column with Pandas?

Given a multiindex row as below w_0 w_1 w_2 w_3 w_4 bar a 1.228191 0.912254 1.976780 -0.441382 1.100406 b -0.149423 -0.559502 0.864034 -0.925317 1.366656 c -1.554960 -1.010015 1.313886 0.183036 …
mpx
  • 3,081
  • 2
  • 26
  • 56
1
vote
2 answers

Melting a dataframe in R

I have dataframe of active users where it's 1 when the user was active this specific date and 0 when he was not. I am trying to modify my dataframe from this: df <- data.frame(userID=c("user123","user124","user125"), …
ML_Enthousiast
  • 1,147
  • 1
  • 15
  • 39
1
vote
2 answers

Spread specific columns in dataframe without any aggregation?

Here is my toy df: {'id': {0: 1089577, 1: 1089577, 2: 1089577, 3: 1089577, 4: 1089577}, 'title': {0: 'Hungarian Goulash Stew', 1: 'Hungarian Goulash Stew', 2: 'Hungarian Goulash Stew', 3: 'Hungarian Goulash Stew', 4: 'Hungarian Goulash…
SteveS
  • 3,789
  • 5
  • 30
  • 64
1
vote
1 answer

Regex colnames and grouping in R?

I have this data frame: id <- c(0,1,2,3,4) groupA_sample1_values <- c(10,11,12,13,14) groupA_sample2_values <- c(20,21,22,23,24) groupA_sample3_values <- c(30,31,32,33,34) groupB_sample1_values <- c(40,41,42,43,44) groupB_sample2_values <-…
CodingBiology
  • 262
  • 4
  • 13
1
vote
2 answers

Pandas melt 2 groups of columns

I have a table as id name sec1 sec2 sec3 sec4 sec5 sec6 1 abc mp(6) up(7) sp(8) cp(7) 2 4 and i want output as id name tests values slots 1 abc mp 6 2 1 abc up 7 2 1 abc sp …
naina
  • 303
  • 3
  • 14
1
vote
3 answers

Melting data, keeping certain columns paired

I have data as follows: DT <- structure(list(ECOST = c("Choice_01", "Choice_02", "Choice_03", "Choice_04", "Choice_05", "Choice_06", "Choice_07", "Choice_08", "Choice_09", "Choice_10", "Choice_11", "Choice_12"), control = c(18, 30, 47, 66, 86,…
Tom
  • 2,173
  • 1
  • 17
  • 44
1
vote
1 answer

Convert 2 column dataframe into a single row with multiple columns

I have a dataframe of values that i'd like to turn into a single row where each key is a column and each value is a the value, with the result being a single row. It seems like it should be a simple task but im struggling to get it done. I…
Michael Lopez
  • 79
  • 2
  • 11
1
vote
2 answers

Reshape Pandas dataframe columns by block of N columns

I have 1 dataframe where blocks of columns need to be reshaped to rows. I tried to use stack() and melt() but could not manage to find the right way. Here is an example of what I expect: data = {'id':['a1', 'a2', 'a3', 'a4'], 'year':[20,…
user2590177
  • 167
  • 1
  • 11
1
vote
3 answers

pandas dataframe how to unmelt column

I have a dataframe: df = key1 key2 .. keyn type val1 val2 .. valn k1 k2 kn p1 1 2 7 k1 k2 kn p2 6 1 5 k1 k2 kn p3 8 4 1 k3 k2 kn p1 4 6 9 …
Cranjis
  • 1,590
  • 8
  • 31
  • 64
1
vote
1 answer

Transform and reshape a Data Frame from wide to long with additional column

I have a data frame that I want to transform from wide into a long format. But I do not want to use all columns. In detail, I want to melt the following data frame import pandas as pd data = {'year': [2014, 2018,2020,2017], 'model':[12,…
chris_tri
  • 67
  • 4
1
vote
2 answers

more dynamic melting with data.table

I am looking for the most efficient form to transform ARTNR FILGRP 1 1 9827 2 2 9348 3 3 9335, 9827, 9339 into this ARTNR FILGRP 1 1 9827 2 2 9348 3 3 9335 4 3 …
mullet
  • 15
  • 7
1
vote
2 answers

R: {reshape}: (melt.data.frame) How do I replicate a column?

I have an array of iterations in an MCMC algorithm. The rows represent draws from a distribution. The columns represent parameters (variables) in the distribution. For ease of exposition: assume two variables, five iterations. So I have: >…
M. Tibbits
  • 8,400
  • 8
  • 44
  • 59
1
vote
2 answers

Need help reshaping an R dataset

My dataset currently looks something like this: id date00 var1_00 var2_00 date01 var1_01 var2_01 1 1/1/2019 1 2 1/1/2020 3 4 2 2/2/2019 1 2 2/2/2020 3 4 3 3/3/2019 1 2 3/3/2020 3 4 The code…
Karen
  • 43
  • 5