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
9
votes
4 answers

Fill area between two lines, with high/low and dates

Forword: I provide a reasonably satisfactory answer to my own question. I understand this is acceptable practice. Naturally my hope is to invite suggestions and improvements. My purpose is to plot two time series (stored in a dataframe with dates…
PatrickT
  • 10,037
  • 9
  • 76
  • 111
9
votes
4 answers

Integer64 class doesn't survive reshape2 melt function

I don't know whether this is an integer64 (from bit64) problem, or a melt problem (from reshape2, but if I try to reshape a data.frame containing integer64 data then the class information is destroyed in the process and it reverts to the double…
Corvus
  • 7,548
  • 9
  • 42
  • 68
9
votes
4 answers

Reshaping data frame with duplicates

I have what should be a simple reshaping problem, but I can't figure it out. Part of my data looks like this: foo <- structure(list(grade = c(3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 3, 3, 4, 4, 5, 5, 6, 6), var.type = structure(c(3L, 2L, 3L, 2L, 3L,…
Stuart
  • 569
  • 6
  • 12
9
votes
4 answers

Select a value for based on a highest value in another column

I don't understand why I can't find a solution for this, since I feel that this is a pretty basic question. Need to ask for help, then. I want to rearrange airquality dataset by month with maximum temp value for each month. In addition I want to…
Mikko
  • 7,530
  • 8
  • 55
  • 92
8
votes
3 answers

gather on first two rows

I have some poorly formatted data that I must work with. It contains two identifiers in the first two rows, followed by the data. The data looks like: V1 V2 V3 1 Date 12/16/18 12/17/18 2 Equip a b 3 x1 1 …
Lloyd Christmas
  • 1,016
  • 6
  • 15
8
votes
2 answers

Error with custom aggregate function for a cast() call in R reshape2

I want to use R to summarize numerical data in a table with non-unique rownames to a result table with unique row-names with values summarized using a custom function. The summarization logic is: use the mean of values if the ratio of the maximum to…
user594694
  • 327
  • 4
  • 13
8
votes
2 answers

How can I gather_ on all columns but one?

I need to gather_ on all columns of a dataframe, except for one. Example: # I want to generate a dataframe whose column names are the letters of the alphabet. If you know of a simpler way, let me know! foo <- as.data.frame(matrix(runif(100), 10,…
DeltaIV
  • 4,773
  • 12
  • 39
  • 86
8
votes
3 answers

From long to wide data with multiple columns

Suggestions for how to smoothly get from foo to foo2 (preferably with tidyr or reshape2 packages)? This is kind of like this question, but not exactly I think, because I don't want to auto-number columns, just widen multiple columns. It's also kind…
dfrankow
  • 20,191
  • 41
  • 152
  • 214
8
votes
1 answer

How do I convert a wide dataframe to a long dataframe for a multilevel structure with 'quadruple nesting'?

I conducted a study that, in retrospect (one lives, one learns :-)) appears to generate multilevel data. Now I'm trying to restructure the dataset from wide to long so that I can analyse it using e.g. lme4. In doing so, I encounter an, um,…
Matherion
  • 657
  • 1
  • 5
  • 13
8
votes
2 answers

Compute matrix of sums

Suppose I have a data.frame with several columns of categorical data, and one column of quantitative data. Here's an example: my_data <- structure(list(A = c("f", "f", "f", "f", "t", "t", "t", "t"), B = c("t", "t", "t",…
arvi1000
  • 9,393
  • 2
  • 42
  • 52
8
votes
4 answers

reshape2: multiple results of aggregation function?

From what I read, *cast operations in reshape2 lost their result_variable feature. Hadley hints at using plyr for this purpose (appending multiple result columns to the input data frame). How would I realize the documentation example ... aqm <-…
mrcalvin
  • 3,291
  • 12
  • 18
8
votes
3 answers

Fast melted data.table operations

I am looking for patterns for manipulating data.table objects whose structure resembles that of dataframes created with melt from the reshape2 package. I am dealing with data tables with millions of rows. Performance is critical. The generalized…
Sim
  • 13,147
  • 9
  • 66
  • 95
8
votes
2 answers

How to reshape a dataframe with "reoccurring" columns?

I am new to data analysis with R. I recently got a pre-formatted environmental observation-model dataset, an example subset of which is shown below: date site obs mod site obs mod 2000-09-01 00:00:00 …
elarry
  • 521
  • 2
  • 7
  • 20
7
votes
4 answers

Is there an alternative to reshape2::melt for multidimensional arrays in base R or tidyverse?

Suppose I have a 3-dimensional array g with dimensions [x,y,z]. reshape2::melt(g) would produce a data frame with columns giving indices of x,y,z and value where value contains the values in each entry of the prior array. Given that reshape2 is…
DanO
  • 600
  • 3
  • 11
7
votes
3 answers

How to replace reshape2::melt for an array with tidyr?

I would like to convert a matrix/array (with dimnames) into a data frame. This can be done very easily using reshape2::melt but seems harder with tidyr, and in fact not really possible in the case of an array. Am I missing something? (In particular…
banbh
  • 1,331
  • 1
  • 13
  • 31