Questions tagged [reshape]

In R, Matlab, NumPy and APL, reshape functions allow data to be transformed into more convenient forms.

Reshape functions allow data to be transformed into more convenient forms.

R

The function reshapes a data frame between ‘wide’ format with repeated measurements in separate columns of the same record and ‘long’ format with the repeated measurements in separate records.

Matlab

The function allows a vector or array to be transformed into a new array with the specified dimensions.
Note that reshape does not change the order of the elements or the number of elements in the array. reshape only affects its shape.

NumPy

The function gives a new shape to an array without changing its data. The returned array will be a new view object if possible; otherwise, it will be a copy.

APL

The function allows any array to be transformed into a new array with the specified shape. Note that does not change the order of the elements, however it can change the number of elements in the array, recycling elements if they are insufficient to fill the requested shape, or truncating trailing elements if the requested shape cannot hold them all.

3858 questions
21
votes
3 answers

Getting a stacked area plot in R

This question is a continuation of the previous question I asked. Now I have a case where there is also a category column with Prop. So, the dataset becomes like Hour Category Prop2 00 A 25 00 B 59 00 A …
sfactor
  • 12,592
  • 32
  • 102
  • 152
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
20
votes
3 answers

Pivot on data.table similar to rehape melt function

I have read some references to similar problems here on SO, but haven't been able to find a solution yet and wondering if there is any way to do the following using just data.table. I'll use a simplified example, but in practice, my data table has >…
xbsd
  • 2,438
  • 4
  • 25
  • 35
20
votes
1 answer

R package reshape function melt error: id variables not found in data when working with a lot of factors

I am working with a rarefaction output from mothur, which basically gives me a dataset containing the number of sequences sampled and the number of unique sequences in several samples. I would like to use ggplot2 to visualize this data and therefore…
FM Kerckhof
  • 1,270
  • 1
  • 14
  • 31
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
3 answers

Convert data from many rows to many columns

I have data that comes out of a DB in a normalized way with a field for year, state, and value. I would like to do analysis on the data and need it formatted where each year is a field and not a record.So I would like the data where each record is a…
JD Long
  • 59,675
  • 58
  • 202
  • 294
18
votes
5 answers

Flatten list column in data frame with ID column

My data frame contains the output of a survey with a select multiple question type. Some cells have multiple values. df <- data.frame(a=1:3,b=I(list(1,1:2,1:3))) df a b 1 1 1 2 2 1, 2 3 3 1, 2, 3 I would like to flatten out the…
mloudon
  • 183
  • 1
  • 4
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
17
votes
7 answers

Reshape data to split column values into columns

df <- data.frame(animal = c("dog", "dog", "cat", "dog", "cat", "cat"), hunger = c(0, 1, 1, 0, 1,1)) I have a dataframe like the one above with two columns, one containing categories and the other containing binary data. I am…
Icewaffle
  • 443
  • 2
  • 13
17
votes
4 answers

reshape a pandas dataframe

suppose a dataframe like this one: df = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]], columns = ['A', 'B', 'A1', 'B1']) I would like to have a dataframe which looks like: what does not work: new_rows = int(df.shape[1]/2) * df.shape[0] new_cols…
Moritz
  • 5,130
  • 10
  • 40
  • 81
17
votes
1 answer

Assignment to empty index (empty square brackets x[]<-) on LHS

While looking at an answer posted recently on SO, I noticed an unfamiliar assignment statement. Instead of the usual form of myVar<- myValue, it used the form myVar[]<- myValue, i.e. the object on lefthand side is indexed with empty square…
R.S.
  • 2,093
  • 14
  • 29
17
votes
3 answers

R re-arrange dataframe: some rows to columns

I'm not even sure how to title the question properly! Suppose I have a dataframe d: Current dataframe: d <- data.frame(sample = LETTERS[1:2], cat = letters[11:20], count = c(1:10)) sample cat count 1 A k 1 2 B l 2 3 …
crs
  • 323
  • 1
  • 2
  • 8
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
17
votes
5 answers

Reshaping data frame in R

I'm running into difficulties reshaping a large dataframe. And I've been relatively fortunate in avoiding reshaping problems in the past, which also means I'm terrible at it. My current dataframe looks something like this: unique_id seq …
Vince
  • 7,608
  • 3
  • 41
  • 46
16
votes
5 answers

melt the lower half matrix in R

How can I melt a lower half triangle plus diagonal matrix ? 11 NA NA NA NA 12 22 NA NA NA 13 23 33 NA NA 14 24 34 44 NA 15 25 35 45 55 A <- t(matrix (c(11, NA, NA, NA, NA, 12, 22, NA, NA, NA, 13, 23, 33, NA, NA, 14, 24, 34, …
jon
  • 11,186
  • 19
  • 80
  • 132