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
30
votes
5 answers

Reshaping an array to data.frame

I have the following data structure (an "atomic vector?") output from daply in plyr, in which I had the function return three different measures for each subject, condition, and item. x = structure(c(-0.93, 0.39, 0.88, 0.63, 0.86, -0.69, 1.02,…
Amyunimus
  • 1,583
  • 4
  • 20
  • 43
29
votes
1 answer

pandas pivot table to data frame

I have a dataframe (df) that looks like this: +---------+-------+------------+----------+ | subject | pills | date | strength | +---------+-------+------------+----------+ | 1 | 4 | 10/10/2012 | 250 | | 1 | 4 |…
alma123
  • 423
  • 1
  • 4
  • 8
29
votes
4 answers

Python reshape list to ndim array

Hi I have a list flat which is length 2800, it contains 100 results for each of 28 variables: Below is an example of 4 results for 2 variables [0, 0, 1, 1, 2, 2, 3, 3] I would like to reshape the list to an array (2,4) so that the results…
BenP
  • 825
  • 1
  • 10
  • 30
29
votes
2 answers

Is this the best way to add an extra dimension to a numpy array in one line of code?

If k is an numpy array of an arbitrary shape, so k.shape = (s1, s2, s3, ..., sn), and I want to reshape it so that k.shape becomes (s1, s2, ..., sn, 1), is this the best way to do it in one line? k.reshape(*(list(k.shape) + [1])
qAp
  • 1,139
  • 2
  • 12
  • 26
29
votes
2 answers

melt to two variable columns

I have the following variables in a data frame: [1] "Type" "I.alt" "idx06" "idx07" "idx08" "farve1" "farve2" If I do: dm <- melt(d, id=c("Type","I.alt")) I get these variables: "Type" "I.alt" "variable" "value" Where "idx06",…
Andreas
  • 6,612
  • 14
  • 59
  • 69
28
votes
5 answers

How to reshape a pandas.Series

It looks to me like a bug in pandas.Series. a = pd.Series([1,2,3,4]) b = a.reshape(2,2) b b has type Series but can not be displayed, the last statement gives exception, very lengthy, the last line is "TypeError: %d format: a number is required,…
szli
  • 36,893
  • 11
  • 32
  • 40
26
votes
3 answers

how to pivot/unpivot (cast/melt) data frame?

How can I 'unpivot' a table? What is the proper technical term for this? UPDATE: The term is called melt I have a data frame for countries and data for each year Country 2001 2002 2003 Nigeria 1 2 3 UK 2 NA …
pedrosaurio
  • 4,708
  • 11
  • 39
  • 53
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
24
votes
3 answers

Intuition and idea behind reshaping 4D array to 2D array in NumPy

While implementing a Kronecker-product for pedagogical reasons (without using the obvious and readily available np.kron()), I obtained a 4 dimensional array as an intermediate result, which I've to reshape to get the final result. But, I still can't…
kmario23
  • 57,311
  • 13
  • 161
  • 150
24
votes
3 answers

Reshape 3d matrix to 2d matrix

I have a 3d matrix (n-by-m-by-t) in MATLAB representing n-by-m measurements in a grid over a period of time. I would like to have a 2d matrix, where the spatial information is gone and only n*m measurements over time t are left (ie: n*m-by-t) How…
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
23
votes
6 answers

How can I change the shape of a variable in TensorFlow?

TensorFlow tutorial says that at creation time we need to specify the shape of tensors. That shape automatically becomes the shape of the tensor. It also says that TensorFlow provides advanced mechanisms to reshape variables. How can I do that?…
Jadiel de Armas
  • 8,405
  • 7
  • 46
  • 62
22
votes
3 answers

ValueError: cannot reshape array of size 30470400 into shape (50,1104,104)

I am trying to run threw this Tutorial http://emmanuelle.github.io/segmentation-of-3-d-tomography-images-with-python-and-scikit-image.html where I want to do a Segmentation of 3-D tomography images with Python. I'm struggling directly in the…
Bananaboy99
  • 231
  • 1
  • 2
  • 4
22
votes
2 answers

Python Reshape 3d array into 2d

I want to reshape the numpy array as it is depicted, from 3D to 2D. Unfortunately, the order is not correct. A assume to have a numpy array (1024, 64, 100) and want to convert it to (1024*100, 64). Does anybody has an idea how to maintain the…
pallago
  • 419
  • 1
  • 4
  • 12
22
votes
2 answers

Convert matrix to three column data.frame

I've got matrix: var1 var2 row1 1 2 row2 3 4 Want to convert it to data.frame: rows vars values row1 var1 1 row1 var2 2 row2 var1 3 row2 var2 4 What is the best way to do it?
Aleksandro M Granda
  • 665
  • 1
  • 8
  • 13
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