Questions tagged [reshape2]

The reshape2 package in R contains functions that allow data to be transformed into more convenient forms.

reshape2 is an package for flexibly restructuring and aggregating data. Its primary use is ing data between wide format with repeated measurements in separate columns of the same record and long format with the repeated measurements in separate records.

reshape2 succeeds the reshape package, and is succeeded by the package.

Repositories

Other resources

Related tags

1177 questions
15
votes
1 answer

How to name each variable using melt

I have a matrix that I want to reform for plotting in ggplo2 using the melt function from reshape2 but cannot find a way to add custom header names. #Create toy data MyData <- matrix(rnorm(15,500), nrow = 5, ncol = 3, dimnames = list( …
Josh R.
  • 449
  • 2
  • 4
  • 13
14
votes
1 answer

Convert from n x m matrix to long matrix in R

Note: This is not a graph question. I have an n x m matrix: > m = matrix(1:6,2,3) > m a b c d 1 2 3 e 4 5 6 I would like to convert this to a long matrix: > m.l a d 1 a e 4 b d 2 b e 5 c d 3 c e 6 Obviously nested for loops would work but…
user3030872
  • 367
  • 1
  • 6
  • 14
13
votes
2 answers

How to change the melt.data.frame function in reshape2 package returned "variable" column to "character" class?

The default behavior of melt.data.frame is to return the "variable" column in "factor" class. Here is an example: > head(airquality) ozone solar.r wind temp month day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12…
rninja
  • 540
  • 1
  • 4
  • 12
13
votes
2 answers

dcast specific column and keep all

I'm trying to take a column that has specific values for each type of element for each type of gridNumber and dcast it so that it creates 3 separate columns from the element column. I'm not sure exactly how to do this. dput: df <-…
Vedda
  • 7,066
  • 6
  • 42
  • 77
13
votes
2 answers

how to spread or cast multiple values in r

Here is toy data set for this example: data <- data.frame(x=rep(c("red","blue","green"),each=4), y=rep(letters[1:4],3), value.1 = 1:12, value.2 = 13:24) x y value.1 value.2 1 red a 1 13 2 red b 2 14 3 red c …
Rokmc1050
  • 463
  • 1
  • 6
  • 16
13
votes
5 answers

Reshape multiple categorical variables to binary response variables

I am trying to convert the following format: mydata <- data.frame(movie = c("Titanic", "Departed"), actor1 = c("Leo", "Jack"), actor2 = c("Kate", "Leo")) movie actor1 actor2 1 Titanic Leo …
ignorant
  • 1,390
  • 1
  • 10
  • 14
12
votes
2 answers

Tidy data.frame with repeated column names

I have a program that gives me data in this format toy file_path Condition Trial.Num A B C ID A B C ID A B C ID 1 root/some.extension Baseline 1 2 3 5 car 2 1 7 bike 4 9 0 plane 2 root/thing.extension …
Matias Andina
  • 4,029
  • 4
  • 26
  • 58
12
votes
1 answer

tidyr::gather vs. reshape2::melt on matrices

I've been a long time user of reshape2::melt in a rather non-standard way: I'm running numeric experiments and get a matrix as a result. I then melt it and produce some images. Inspired by the similarity between reshape2 and tidyr, I'm now trying to…
tonytonov
  • 25,060
  • 16
  • 82
  • 98
12
votes
9 answers

split characters into two variables in data frame

Let's say I have a vector of variables like this: >variable [1] "A1" "A1" "A1" "A1" "A2" "A2" "A2" "A2" "B1" "B1" "B1" "B1" and I want to covert this into into a data frame like this: treatment time 1 A 1 2 A 1 3 A …
Alby
  • 5,522
  • 7
  • 41
  • 51
12
votes
2 answers

Generate graphs in R for certain correlations in a matrix

I want to generate graphs between variables (columns) that have a correlation above and below a certain point as well as having a pvalue < 0.01. The graphs would be ggplot2 (line or bar) graphs plotting the two columns (variables) that…
themartinmcfly
  • 2,004
  • 2
  • 13
  • 12
11
votes
1 answer

I am unable to download the reshape2 package in R

I am getting this response when trying to install the R package: >installed.packages("reshape2") Package LibPath Version Priority Depends Imports LinkingTo Suggests Enhances License License_is_FOSS License_restricts_use OS_type Archs…
ayush garg
  • 129
  • 1
  • 1
  • 4
11
votes
1 answer

R: Efficiently remove singleton dimensions from array

I am looking for a fast way to remove redundant dimensions from an array in R, similar to the squeeze() command in MATLAB. Right now I combine the melt() and the cast() commands from the reshape2 package, but there should be a less intricate way of…
11
votes
1 answer

Using melt with matrix or data.frame gives different output

Consider the following code: set.seed(1) M = matrix(rnorm(9), ncol = 3) dimnames(M) = list(LETTERS[1:3], LETTERS[1:3]) print(M) A B C A -0.6264538 1.5952808 0.4874291 B 0.1836433 0.3295078 0.7383247 C -0.8356286…
Fernando
  • 7,785
  • 6
  • 49
  • 81
11
votes
2 answers

melt multiple groups of measure.vars

I have a data.table containing a number of variables across multiple years, i.e: > dt <- data.table(id=1:3, A_2011=rnorm(3), A_2012=rnorm(3), B_2011=rnorm(3), B_2012=rnorm(3), C_2011=rnorm(3),…
Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
11
votes
3 answers

segfault in R using reshape2 package and dcast

RStudio was crashing when I tried to reshape a particular data frame using dcast (from the reshape2 package). I discovered that the crash was actually happening in R itself, so I ran my casting code in R.app and got the type of error that gives this…
eipi10
  • 91,525
  • 24
  • 209
  • 285
1 2
3
78 79