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
6
votes
2 answers

Is there a melt command in Snowflake?

Is there a Snowflake command that will transform a table like this: a,b,c 1,10,0.1 2,11,0.12 3,12,0.13 to a table like this: key,value a,1 a,2 a,3 b,10 b,11 b,13 c,0.1 c,0.12 c,0.13 ? This operation is often called melt in other tabular systems,…
Haterind
  • 1,095
  • 1
  • 8
  • 16
6
votes
1 answer

Emulating reshape2::melt with pivot_longer for matrixes

i just tried to use pivot_longer on a 2D Matrix to get "tidy" data for ggplot. So far that was pretty straight forward with reshape2::melt library(tidyverse) library(reshape2) x <- c(1, 2, 3, 4) y <- c(1, 2, 3) Data <- matrix(round(rnorm(12,…
SebSta
  • 476
  • 2
  • 12
6
votes
3 answers

Pandas 'partial melt' or 'group melt'

I have a DataFrame like this >>> df = pd.DataFrame([[1,1,2,3,4,5,6],[2,7,8,9,10,11,12]], columns=['id', 'ax','ay','az','bx','by','bz']) >>> df id ax ay az bx by bz 0 1 1 2 3 4 5 6 1 2 7 8 9 10 …
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
6
votes
1 answer

Exact inverse of pandas' "pivot" operation

I have a pandas dataframe in the rough format print(df) Time GroupA GroupB Value1 Value2 0 100.0 1.0 1.0 18.0 0.0 1 100.0 1.0 2.0 16.0 0.0 2 100.0 2.0 1.0 18.0 0.0 3 100.0 2.0 2.0 …
jwimberley
  • 1,696
  • 11
  • 24
6
votes
1 answer

R: Collapse multiple boolean columns into single attribute column with new rows for each combination

Trying to melt or collapse a dataframe with multiple boolean columns into a two column database with an id column and a column for the collapsed values BUT each value results in a new row. Example beginning: A S1 S2 S3 S4 1 ex1 1 0 0 0 2…
Sean Kelso
  • 77
  • 3
6
votes
2 answers

R: Pivot the rows into columns and use N/A's for missing values

I have a dataframe that looks something like this NUM <- c("45", "45", "45", "45", "48", "50", "66", "66", "66", "68") Type <- c("A", "F", "C", "B", "D", "A", "E", "C", "F", "D") Points <- c(9.2,60.8,22.9,1012.7,18.7,11.1,67.2,63.1,16.7,58.4) df1…
Sharath
  • 2,225
  • 3
  • 24
  • 37
6
votes
1 answer

How to melt R data.frame and plot group by bar plot

I have following R data.frame: group match unmatch unmatch_active match_active 1 A 10 4 0 0 2 B 116 20 0 3 3 c 160 27 1 4 4 D 79 17 …
add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
6
votes
4 answers

How to strsplit data frame column and replicate rows accordingly?

I have a data frame like this: > df <- data.frame(Column1=c("id1", "id2", "id3"), Column2=c("text1,text2,text3", "text4", "text5,text6"), Column3=c("text7", "text8,text9,text10,text11", "text12,text13")) > df Column1 Column2 …
enricoferrero
  • 2,249
  • 1
  • 23
  • 28
6
votes
2 answers

Reshape package masking preventing melt from naming columns

I have a script which requires both reshape and reshape2 libraries. I know this is poor practise, but I think plyr (or another library I am using) Vennerable is loading reshape and I have personally used reshape2 in a lot of places. The problem is…
MattLBeck
  • 5,701
  • 7
  • 40
  • 56
5
votes
1 answer

tidyverse analog of reshape2::melt that does everything?

The answers to this question document that the tidyverse analog of reshape2::melt() is tidyr::pivot_longer(). That's true, as far as it goes, but I still find the process of melting a matrix with defined dimnames in tidyverse much less convenient…
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
5
votes
9 answers

Wide to long data in R

I have following data in R: structure(list(Name = 1:4, Paper1 = c("C1", "C1", "C1", "C1"), Marks1 = 1:4, Paper2 = c("D1", "D1", "D1", "D1"), Marks2 = 1:4, Paper3 = c("E1", "E1", "E1", "E1"), Marks3 = 12:15), class = "data.frame", row.names = c(NA,…
Neeraj
  • 1,166
  • 9
  • 21
5
votes
1 answer

Seeking R function to melt 5-dimensional array, like pivot_longer

I have a program that uses reshape2's melt function to melt a 5-dimensional array with named and labelled dimensions to a long-form data frame, which by definition has only two dimensions. Each dimension of the input array corresponds to a column in…
Andrew Kirk
  • 2,027
  • 2
  • 11
  • 16
5
votes
5 answers

melt column by substring of the columns name in pandas (python)

I have dataframe: subject A_target_word_gd A_target_word_fd B_target_word_gd B_target_word_fd subject_type 1 1 2 3 4 mild 2 …
Cranjis
  • 1,590
  • 8
  • 31
  • 64
5
votes
2 answers

How to specify an empty id.vars vector when melting a data.table?

I'd like to melt a data.table without including any ID columns. dt <- data.table::data.table(iris)[1:10] data.table::melt(dt, measure.vars=c('Petal.Length', 'Petal.Width')) Without specifying id.vars, all of the non measure.vars columns are treated…
logworthy
  • 1,218
  • 2
  • 11
  • 23
5
votes
1 answer

pandas melt dataframe according to time index

I have the following dataframe that I try to 'melt'. So my aim is to get a output with 2 column Column name Value So my output should look like [that's only the head of the output, I don't show it fully to be concise] I have tried the following…
S12000
  • 3,345
  • 12
  • 35
  • 51
1 2
3
55 56