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

Melting large dataframes--is there a practical size limit for reshape2's melt?

The problem I am trying to reshape a survey dataset loaded into a dataframe with about 11k variables and 2k rows into a long(er) format, in order to do some analysis on variables that resulted from looped questions. I have not been able to figure…
maitagorri
  • 41
  • 5
1
vote
1 answer

How do I not lose all non-melted columns when melting in data.table?

This is a very basic question, but I was wondering how I don't lose all the other columns in my dataset dt when I melt on the few melted variables. I want the other variables to remain in the dataset, with the same values they had in dt according to…
davidk
  • 133
  • 2
  • 11
1
vote
2 answers

Create subgroups in data.table

Say i have the following simplified dataset: dt <- data.table(id = 1:5, val = c(1, 2, 3, 2, 4)) dt2 <- data.table(id = c(2, 4), val = c(2, 3)) I want to replace all values in dt which have the value 2. The replacing value is given in dt2. The two…
Tlatwork
  • 1,445
  • 12
  • 35
1
vote
0 answers

how to expand rows by shrinking columns

I want to convert the name of last three columns into a new column called 'medal' and create a new column called 'no of medals' which is the corresponding value according to different medals I know how to convert rows to columns by using pivot…
lidito
  • 11
  • 2
1
vote
1 answer

Difficulty with Long to Wide to Long Format in R

I am trying to make a graph similar to a correlation plot. However, my data was in long format and I wanted to only show the lower triangle of the matrix. Therefore, I took my data and reshaped it using the…
Jack Armstrong
  • 1,182
  • 4
  • 26
  • 59
1
vote
1 answer

how to convert multiple columns and their headers into rows

I have a dataframe with 28 column, 27 of which are datetime columns, 1 of which is an ID column. I need to convert the dataset into 3 total columns where there's one datatime column, a second column with the original header from which the datetime…
Mr. Jibz
  • 511
  • 2
  • 7
  • 21
1
vote
2 answers

Reshape to long and complete with NAs

I need to reshape a df, complete it with missing years and create a variable tracking status change. The issue is that some values are missing and the code I wrote brakes down on those. Toy example: library(data.table) df <-…
MCS
  • 1,071
  • 9
  • 23
1
vote
1 answer

Recast in R gives different value

I have the following dataframe in R DF2<-data.frame("ID"=c("A", "A", "A", "B", "B", "B", "B", 'B'), 'Freq'=c(1,2,3,1,2,3,4,5), "Val"=c(1,2,4, 2,3,4,5,8)) The datframe has the following appearance ID Freq Val 1 A 1 1 2 A 2 2 3 A…
Raghavan vmvs
  • 1,213
  • 1
  • 10
  • 29
1
vote
2 answers

A melt call that resembles a transpose

I'm wondering whether my two step solution can be reduced to one melt() call or done more simply somehow using data.table. My data: DT <- data.table(category = c("x", "y"), `2010` = c(10, 20), `2011` = c(40, 43)) DT # Has much more columns in…
s_baldur
  • 29,441
  • 4
  • 36
  • 69
1
vote
2 answers

Forcing upper triangle values to lower triangle values in matrix after melt

I have matrix m = matrix(c(0,-1,-2,-3,1,0,-4,-5,2,4,0,-6,3,5,6,0),4,4) m which is not symmetric initially [,1] [,2] [,3] [,4] [1,] 0 1 2 3 [2,] -1 0 4 5 [3,] -2 -4 0 6 [4,] -3 -5 -6 0 I need to melt…
Heikki
  • 2,214
  • 19
  • 34
1
vote
1 answer

How do I change the structure of a time-series table from row to column?

So I have a table that is in the following format: date <- c('t1','t1','t1','t2','t2','t2') factor <- c('a','b','c','a','b','c') beta <- c(1,2,4,3,5,2) df <- data.frame(date, factor, beta) df date factor beta 1 t1 a 1 2 t1 b …
Erwin Rhine
  • 303
  • 2
  • 11
1
vote
3 answers

R: Melting an data frame and plotting by group

I have a large dataset that I'd like to use to plot genetic divergence along chromosomes. The data frame I am using has the following format. ID Group 100 270 310 430 460 550 580 660 710 740 Strain1 A …
DrDNA
  • 11
  • 2
1
vote
2 answers

Plotting all observations but colour them based on different group

I have a sample data given below with sample IDs which are unique, and 3 groups. I need to plot all the observations (rows) in 'df' but color them according to the group IDs ('groupid'). Here's what I have so far: # sample data creation samples <-…
snair.stack
  • 405
  • 4
  • 13
1
vote
1 answer

Converting a nested (3-level) list to a long/tall format data frame

I have a nested list with 3 levels: m = list(try1 = list(list(court = c("jack", "queen", "king"), suit = list(diamonds = 2, clubs = 5)), list(court = c("jack", "queen", "king"), …
user51462
  • 1,658
  • 2
  • 13
  • 41
1
vote
2 answers

Long to wide with different number of rows for each group/ID

I have the following data frame: df <- data.frame(stringsAsFactors=FALSE, ID = c(2016070707, 2016070707, 2016070707, 2017080808, 2017080808, 2017080808, 2017080808), team_name = c("Harlequins",…
Rama
  • 25
  • 4