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

Pivoting rows into columns

Suppose (to simplify) I have a table containing some control vs. treatment data: Which, Color, Response, Count Control, Red, 2, 10 Control, Blue, 3, 20 Treatment, Red, 1, 14 Treatment, Blue, 4, 21 For each color, I want a single row with the…
grautur
  • 29,955
  • 34
  • 93
  • 128
13
votes
3 answers

Normalize (reformat) cross-tab data for Tableau without using Excel

Tableau generally works best when input data is in "normalized" format, rather than cross-tab. This is also referred to as converting from "wide format" to "long format". That is, converting from: To: Tableau provides a "reshaping tool" for Excel…
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
13
votes
5 answers

Reshape multiple categorical variables to binary response variables

I am trying to convert the following format: mydata <- data.frame(movie = c("Titanic", "Departed"), actor1 = c("Leo", "Jack"), actor2 = c("Kate", "Leo")) movie actor1 actor2 1 Titanic Leo …
ignorant
  • 1,390
  • 1
  • 10
  • 14
13
votes
3 answers

R data.table grouping for lagged regression

table with data (its a data.table object) that looks like the following : date stock_id logret 1: 2011-01-01 1 0.001 2: 2011-01-02 1 0.003 3: 2011-01-03 1 0.005 4: 2011-01-04 1 0.007 5:…
user1480926
  • 634
  • 5
  • 12
12
votes
4 answers

Pivot Table-like Output in R?

I am writing a report that requires the generation of a number of pivot tables in Excel. I would like to think there is a way to do this in R so that I can avoid Excel. I would like output like the screenshot below (teacher names redacted). As far…
Jeff Erickson
  • 3,783
  • 8
  • 36
  • 43
12
votes
2 answers

Tidy data.frame with repeated column names

I have a program that gives me data in this format toy file_path Condition Trial.Num A B C ID A B C ID A B C ID 1 root/some.extension Baseline 1 2 3 5 car 2 1 7 bike 4 9 0 plane 2 root/thing.extension …
Matias Andina
  • 4,029
  • 4
  • 26
  • 58
12
votes
4 answers

Easy way to convert long to wide format with counts

I have the following data set: sample.data <- data.frame(Step = c(1,2,3,4,1,2,1,2,3,1,1), Case = c(1,1,1,1,2,2,3,3,3,4,5), Decision =…
dGecko
  • 153
  • 2
  • 7
12
votes
3 answers

Numpy - slicing 2d row or column vector from array

I'm trying to find a neat little trick for slicing a row/column from a 2d array and obtaining an array of (col_size x 1) or (1 x row_size). Is there an easier way than to use numpy.reshape() after every slicing? Cheers, Stephan
neurotronix
  • 221
  • 1
  • 2
  • 11
12
votes
7 answers

How to create a pivot table in R with multiple (3+) variables

I am having problems in create a pivot table with a data frame like this: c1 c2 c3 c4 E 5.76 201 A la vista E 47530.71 201 A la vista E 82.85 201 A la vista L 11376.55 201 A la vista E …
Duck
  • 39,058
  • 13
  • 42
  • 84
12
votes
4 answers

stacking columns into 1 column in R

I have a data frame that looks like: ID Time U1 U2 U3 U4 ... 1 20 1 2 3 5 .. 2 20 2 5 9 4 .. 3 20 2 5 6 4 .. . . And I would need to keep it like: ID Time U 1 20 1 1 20 2 1 20 3 1 20 5 2 20 2 2 20 5 2 …
user2263330
  • 121
  • 1
  • 1
  • 3
11
votes
2 answers

Fastest way to reshape variable values as columns

I have a dataset with about 3 million rows and the following structure: PatientID| Year | PrimaryConditionGroup --------------------------------------- 1 | Y1 | TRAUMA 1 | Y1 | PREGNANCY 2 | Y2 | SEIZURE 3 | Y1 |…
Matt
  • 17,290
  • 7
  • 57
  • 71
11
votes
6 answers

split character columns and get names of field in string

I need to split a column that contains information into several columns. I'd use tstrsplit but the same kind of information is not in the same order among the rows and I need to extract the name of the new column within the variable. Important to…
Cath
  • 23,906
  • 5
  • 52
  • 86
11
votes
1 answer

TensorFlow tf.reshape Fortran order (like numpy)

Does TensorFlow provide a way to reshape a tensor in Fortran (column-major order? NumPy allows: a = ... np.reshape(a, (32, 32, 3), order='F') I'm trying to reshape CIFAR images to be 32x32x3 (from a vector of shape 3072x1), but I'm getting images…
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
11
votes
3 answers

Reshaping a pandas correlation matrix

I have the following correlation matrix which was created using pandas: df.corr() symbol aaa bbb ccc ddd eee symbol aaa 1.000000 0.346099 0.131874 -0.150910 …
darkpool
  • 13,822
  • 16
  • 54
  • 89
11
votes
1 answer

Python pandas pivot from long to wide

My data is currently in a long format. Below is a sample: Stock Date Time Price Year AAA 2001-01-05 15:20:09 2.380 2001 AAA 2002-02-23 10:13:24 2.440 2002 AAA 2002-02-27 …
alexalexalex
  • 147
  • 2
  • 2
  • 7