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

How does numpy.swapaxes work?

I created a sample array: a = np.arange(18).reshape(9,2) On printing, I get this as output: [[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11] [12 13] [14 15] [16 17]] On executing this reshaping: b = a.reshape(2,3,3).swapaxes(0,2) I get: [[[ 0 …
phoenix
  • 163
  • 1
  • 1
  • 5
16
votes
2 answers

Reshape data frame to convert factors into columns in R

I have a data frame where one particular column has a set of specific values (let's say, 1, 2, ..., 23). What I would like to do is to convert from this layout to the one, where the frame would have extra 23 (in this case) columns, each one…
Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
16
votes
3 answers

reshape wide to long with character suffixes instead of numeric suffixes

Inspired by a comment from @gsk3 on a question about reshaping data, I started doing a little bit of experimentation with reshaping data where the variable names have character suffixes instead of numeric suffixes. As an example, I'll load the…
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
16
votes
4 answers

Convert a matrix with dimnames into a long format data.frame

Hoping there's a simple answer here but I can't find it anywhere. I have a numeric matrix with row names and column names: # 1 2 3 4 # a 6 7 8 9 # b 8 7 5 7 # c 8 5 4 1 # d 1 6 3 2 I…
Ina
  • 4,400
  • 6
  • 30
  • 44
15
votes
1 answer

Does numpy reshape create a copy?

Is there a way to do a reshape on numpy arrays but inplace. My problem is that my array is very big so any unnecessary copies strain the memory. My current approach is like this: train_x = train_x.reshape(n,32*32*3) this doesn't exactly solve the…
Guilherme de Lazari
  • 1,134
  • 2
  • 11
  • 26
15
votes
2 answers

How does numpy.reshape() with order = 'F' work?

I thought I understood the reshape function in Numpy until I was messing around with it and came across this example: a = np.arange(16).reshape((4,4)) which returns: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], …
Nick-H
  • 342
  • 1
  • 3
  • 15
15
votes
4 answers

Wrapping base R reshape for ease-of-use

It is a truth universally acknowledged that R's base reshape command is speedy and powerful but has miserable syntax. I have therefore written a quick wrapper around it which I will throw into the next release of the taRifx package. Before I did…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
14
votes
1 answer

R: Changing column names in pivot_wider() -- suffix to prefix

I'm trying to figure out how to alter the way in which tidyr's pivot_wider() function creates new variable names in resulting wide data sets. Specifically, I would like the "names_from" variable to be added to the prefix of the new variables rather…
mkpcr
  • 431
  • 1
  • 3
  • 13
14
votes
3 answers

parallel pivot_longer of two sets of columns

I have the following data frame: library(tidyverse) dat <- tribble( ~Scenario, ~V1, ~V2, ~V3, ~V4, 1, 0.97, 0.46, 0.79, 0.25, 1, 0.21, 0.45, 0.23, 0.63, 1, 0.95, 0.97, 0.07, 0.61, 1, 0.93, 0.79,…
deschen
  • 10,012
  • 3
  • 27
  • 50
14
votes
1 answer

Pandas adding Time column to Date index

I have a dataframe, Date index type is Timestamp, Time column is datetime.Time: Time Value Date 2004-05-01 0:15 3.58507 2004-05-02 0:30 3.84625 ... How do I convert it to: Value Date 2004-05-01…
alexbk66
  • 362
  • 1
  • 3
  • 12
14
votes
3 answers

Long and wide data – when to use what?

I'm in the process of compiling data from different data sets into one data set for analysis. I'll be doing data exploration, trying different things to find out what regularities may be hidden in the data, so I don't currently have a specific…
user1322720
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
14
votes
2 answers

stacking columns in data.frame into one column in R

I'm having trouble stacking columns in a data.frame into one column. Now my data looks something like this: id time black white red a 1 b1 w1 r1 a 2 b2 w2 r2 a 3 b3 w3 r3 b 1 …
econlearner
  • 351
  • 2
  • 3
  • 10
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

Fastest Tall-Wide pivoting in R

I am dealing with a simple table of the form date variable value 1970-01-01 V1 0.434 1970-01-01 V2 12.12 1970-01-01 V3 921.1 1970-01-02 V1 -1.10 1970-01-03 V3 0.000 1970-01-03 V5 …
gappy
  • 10,095
  • 14
  • 54
  • 73