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
54
votes
8 answers

Reshaping multiple sets of measurement columns (wide format) into single columns (long format)

I have a dataframe in a wide format, with repeated measurements taken within different date ranges. In my example there are three different periods, all with their corresponding values. E.g. the first measurement (Value1) was measured in the period…
daj
  • 6,962
  • 9
  • 45
  • 79
50
votes
1 answer

reshape vs. reshape2 in R

I am attempting to understand why development had shifted from reshape to reshape2 package. They seem to be functionally the same, however, I am unable to upgrade to reshape2 currently due to an older version of R running on the server. I am…
Alex
  • 19,533
  • 37
  • 126
  • 195
46
votes
3 answers

How to melt and cast dataframes using dplyr?

Recently I am doing all my data manipulations using dplyr and it is an excellent tool for that. However I am unable to melt or cast a data frame using dplyr. Is there any way to do that? Right now I am using reshape2 for this purpose. I want 'dplyr'…
Koundy
  • 5,265
  • 3
  • 24
  • 37
45
votes
6 answers

What does -1 mean in pytorch view?

As the question says, what does -1 do in pytorch view? >>> a = torch.arange(1, 17) >>> a tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16.]) >>> a.view(1,-1) tensor([[ 1., 2., 3., …
aerin
  • 20,607
  • 28
  • 102
  • 140
44
votes
3 answers

Reshape an array in NumPy

Consider an array of the following form (just an example): [[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11] [12 13] [14 15] [16 17]] It's shape is [9,2]. Now I want to transform the array so that each column becomes a shape [3,3], like…
user1876864
  • 465
  • 1
  • 4
  • 6
43
votes
5 answers

Reshaping wide to long with multiple values columns

I need to reshape my wide table into long format but keeping multiple fields for each record, for example: dw <- read.table(header=T, text=' sbj f1.avg f1.sd f2.avg f2.sd blabla A 10 6 50 10 bA B 12 5 70 11 …
Vasily A
  • 8,256
  • 10
  • 42
  • 76
40
votes
1 answer

What methods can we use to reshape VERY large data sets?

When due to very large data calculations will take a long time and, hence, we don't want them to crash, it would be valuable to know beforehand which reshape method to use. Lately, methods for reshaping data have been further developed regarding…
jay.sf
  • 60,139
  • 8
  • 53
  • 110
39
votes
3 answers

can the value.var in dcast be a list or have multiple value variables?

In the help files for dcast.data.table, there is a note stating that a new feature has been implemented: "dcast.data.table allows value.var column to be of type list" I take this to mean that one can have multiple value variables within a list, i.e.…
AlexR
  • 791
  • 2
  • 8
  • 22
36
votes
3 answers

Mysql, reshape data from long / tall to wide

I have data in a mysql table in long / tall format (described below) and want to convert it to wide format. Can I do this using just sql? Easiest to explain with an example. Suppose you have information on (country, key, value) for M countries, N…
chongman
  • 2,447
  • 4
  • 21
  • 23
35
votes
3 answers

pandas pivot_table column names

For a dataframe like this: d = {'id': [1,1,1,2,2], 'Month':[1,2,3,1,3],'Value':[12,23,15,45,34], 'Cost':[124,214,1234,1324,234]} df = pd.DataFrame(d) Cost Month Value id 0 124 1 12 1 1 214 2 23 1 2 …
muon
  • 12,821
  • 11
  • 69
  • 88
34
votes
1 answer

Why reshape2's Melt cannot capture rownames in the transformation?

I have the following data: Cubn 3.71455160837536 0.237454645363458 Gm9779 2.56051657980096 0.20850752817264 Apod 3.51796703048962 0.195999214485821 What I want to do is to create the 'melted' data such that it gives this var1…
neversaint
  • 60,904
  • 137
  • 310
  • 477
34
votes
5 answers

Reshape multiple value columns to wide format

I have the following data frame and i want to use cast to create a "pivot table" with columns for two values (value and percent). Here is the data frame: expensesByMonth <- structure(list(month = c("2012-02-01", "2012-02-01", "2012-02-01",…
Alex Burdusel
  • 3,015
  • 5
  • 38
  • 49
32
votes
7 answers

Compute mean and standard deviation by group for multiple variables in a data.frame

Edit -- This question was originally titled << Long to wide data reshaping in R >> I'm just learning R and trying to find ways to apply it to help out others in my life. As a test case, I'm working on reshaping some data, and I'm having trouble…
user2348358
  • 321
  • 1
  • 3
  • 3
31
votes
2 answers

dcast warning: ‘Aggregation function missing: defaulting to length’

My df looks like this: Id Task Type Freq 3 1 A 2 3 1 B 3 3 2 A 3 3 2 B 0 4 1 A 3 4 1 B 3 4 2 A 1 4 2 B 3 I want to restructure by Id and…
user3315563
  • 495
  • 2
  • 5
  • 10
30
votes
9 answers

Transpose / reshape dataframe without "timevar" from long to wide format

I have a data frame that follows the below long Pattern: Name MedName Name1 atenolol 25mg Name1 aspirin 81mg Name1 sildenafil 100mg Name2 atenolol 50mg Name2 enalapril 20mg And would like to get below (I do not…
Hotamd6
  • 315
  • 3
  • 8