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

R reshape a vector into multiple columns

Let's say I have a vector in R as follows: d<-seq(1,100) I want to reshape this vector into a 10x10 matrix, so that I will have this data instead: [,1] [,2] [,3] .. [,10] 1 2 3 .. 10 11 12 13 .. 20 21 22 23 …
Vahid Mirjalili
  • 6,211
  • 15
  • 57
  • 80
23
votes
8 answers

quick/elegant way to construct mean/variance summary table

I can achieve this task, but I feel like there must be a "best" (slickest, most compact, clearest-code, fastest?) way of doing it and have not figured it out so far ... For a specified set of categorical factors I want to construct a table of means…
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
22
votes
3 answers

Reshaping a data frame with more than one measure variable

I'm using a data frame similar to this one: df<-data.frame(student=c(rep(1,5),rep(2,5)), month=c(1:5,1:5), quiz1p1=seq(20,20.9,0.1),quiz1p2=seq(30,30.9,0.1), quiz2p1=seq(80,80.9,0.1),quiz2p2=seq(90,90.9,0.1)) print(df) …
eli-k
  • 10,898
  • 11
  • 40
  • 44
20
votes
1 answer

R reshape2 'Aggregation function missing: defaulting to length'

I have seen this reshape2 several times on SO but haven't seen a solution to my particular problem; I have a dataset like this; head(data) student test score Adam Exam1 80 Adam Exam2 90 John Exam1 70 John Exam2 …
chattrat423
  • 603
  • 2
  • 11
  • 24
20
votes
2 answers

Reshape multiple values at once

I have a long data set I would like to make wide and I'm curious if there is a way to do this all in one step using the reshape2 or tidyr packages in R. The data frame df looks like this: id type transactions amount 20 income 20 …
Dirk Calloway
  • 2,569
  • 4
  • 23
  • 34
19
votes
1 answer

Make the `drop` argument in `dcast` only look at the RHS of the formula

The drop argument in dcast (from "reshape2" or "dplyr") can be useful when going from a "long" to a "wide" dataset and you want to create columns even for combinations that do not exist in the long form. It turns out that using drop also affects…
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
19
votes
8 answers

Complicated reshaping

I want to reshape my dataframe from long to wide format and I loose some data that I'd like to keep. For the following example: df <- data.frame(Par1 = unlist(strsplit("AABBCCC","")), Par2 = unlist(strsplit("DDEEFFF","")), …
Vasily A
  • 8,256
  • 10
  • 42
  • 76
19
votes
4 answers

melt / reshape in excel using VBA?

I'm currently adjusting to a new job where most of the work I share with colleagues is via MS Excel. I am using pivot tables frequently, and therefore need "stacked" data, precisely the output of the melt() function in the reshape (reshape2)…
baha-kev
  • 3,029
  • 9
  • 33
  • 31
18
votes
4 answers

Faster ways to calculate frequencies and cast from long to wide

I am trying to obtain counts of each combination of levels of two variables, "week" and "id". I'd like the result to have "id" as rows, and "week" as columns, and the counts as the values. Example of what I've tried so far (tried a bunch of other…
user592419
  • 5,103
  • 9
  • 42
  • 67
18
votes
2 answers

Transposing data frames

Happy Weekends. I've been trying to replicate the results from this blog post in R. I am looking for a method of transposing the data without using t, preferably using tidyr or reshape. In example below, metadata is obtained by transposing…
ExperimenteR
  • 4,453
  • 1
  • 15
  • 19
18
votes
2 answers

How to "unmelt" data with reshape r

I have a data frame that I melted using the reshape package that I would like to "un melt". here is a toy example of the melted data (real data frame is 500x100 or larger)…
LP_640
  • 579
  • 1
  • 5
  • 17
18
votes
2 answers

could not find function "cast" despite reshape2 installed and loaded

In following Hadley Wickham's "Practical tools for exploring data and models" examples in section 2.4, Casting molten data I'm getting Error: could not find function "cast". I have the reshape2 package installed and loaded. > cast(msmithsm, time +…
Jack Frost
  • 2,151
  • 3
  • 17
  • 21
17
votes
4 answers

Reshape wide format, to multi-column long format

I want to reshape a wide format dataset that has multiple tests which are measured at 3 time points: ID Test Year Fall Spring Winter 1 1 2008 15 16 19 1 1 2009 12 13 27 1 2 2008 22 22 …
Sam
  • 281
  • 4
  • 10
16
votes
2 answers

in R, customize names of columns created by dcast.data.table

I am new to reshape2 and data.table and trying to learn the syntax. I have a data.table that I want to cast from multiple rows per grouping variable(s) to one row per grouping variable(s). For simplicity, let's make it a table of customers, some of…
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
15
votes
3 answers

R List of lists to dataframe with list name as extra column

I have a list of lists that have names. I want to add them all together into a dataframe but keep all the columns past_earnings_lists[1] successfully returns one list from the list of lists names(past_earnings_lists)[1] successfully returns the…
Bryanzpope
  • 957
  • 1
  • 9
  • 20
1
2
3
78 79