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

melt multiple groups of measure.vars

I have a data.table containing a number of variables across multiple years, i.e: > dt <- data.table(id=1:3, A_2011=rnorm(3), A_2012=rnorm(3), B_2011=rnorm(3), B_2012=rnorm(3), C_2011=rnorm(3),…
Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
11
votes
2 answers

Replace NA's in R - works in a practice dataset but warning message when applied to actual data

I have a dataset in R which looks like, and has been reshaped in the same way as, the following example. The aim is to turn NA values in to something else (e.g. "FALSE" or "0") which can then be used to create a new column…
rg255
  • 4,119
  • 3
  • 22
  • 40
10
votes
3 answers

Input to reshape is a tensor with 37632 values, but the requested shape has 150528

I have the same question:Input to reshape is a tensor with 37632 values, but the requested shape has 150528. writer = tf.python_io.TFRecordWriter("/home/henson/Desktop/vgg/test.tfrecords") # 要生成的文件 for index, name in enumerate(classes): …
Henson Wells
  • 101
  • 1
  • 1
  • 4
10
votes
6 answers

Reshape pandas dataframe from rows to columns

I'm trying to reshape my data. At first glance, it sounds like a transpose, but it's not. I tried melts, stack/unstack, joins, etc. Use Case I want to have only one row per unique individual, and put all job history on the columns. For clients, it…
June
  • 720
  • 10
  • 22
10
votes
6 answers

Reconstruct symmetric matrix from values in long-form

I have a tsv that looks like this (long-form): one two value a b 30 a c 40 a d 20 b c 10 b d 05 c d 30 I'm trying to get this into a dataframe for R (or pandas) a b c d a …
jwillis0720
  • 4,329
  • 8
  • 41
  • 74
10
votes
5 answers

Break list into rows while preserving identifiers in r

I'm working with the following type of dataset names<-c("Aname","Aname","Bname","Cname","Cname") list <- list( c('a, b','b, r','c, g'), c('d,g','e,j'), c('d, h','s, q','f,q'), c('d,r ','s, z'),c('d, r','d, r')) data<-cbind(names,…
Chris Bail
  • 307
  • 1
  • 8
10
votes
1 answer

How to use dcast.data.table with formula as string

I want to use cast for a data.table with formula with name of columns as string My table: c1 c2 c3 1 A 1 1 B 2 1 C 3 2 A1 1 2 B1 2 2 C1 3 I want to have result: c1 1 2 3 1 A B …
Minh Ha Pham
  • 2,566
  • 2
  • 28
  • 43
10
votes
1 answer

Can't reshape numpy array

I have a function that is supposed to take a 1D array of integers and shapes it into a 2D array of 1x3 arrays. It then is supposed to take each 1x3 array and shift it into a 3x1 array. The result is supposed to be a 2D array of 3x1 arrays. Here is…
Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
10
votes
4 answers

How do I add rows and columns to a NUMPY array?

Hello I have a 1000 data series with 1500 points in each. They form a (1000x1500) size Numpy array created using np.zeros((1500, 1000)) and then filled with the data. Now what if I want the array to grow to say 1600 x 1100? Do I have to add arrays…
Thomas Browne
  • 23,824
  • 32
  • 78
  • 121
10
votes
1 answer

R's reshape() function changes column labels

I want to convert my data from long into wide format. If find it really annoying that reshape changes the column labels, is there any way to avoid that? For example if I would have data in long format like this: Year Name Value 1996 a 1 1997 a …
user2015601
9
votes
5 answers

How do I unpack tuple format in R?

Here is the dataset. library(data.table) x <- structure(list(id = c("A", "B" ), segment_stemming = c("[('Brownie', 'Noun'), ('From', 'Josa'), ('Pi', 'Noun')]", "[('Dung-caroon-gye',…
jhyeon
  • 456
  • 4
  • 14
9
votes
3 answers

tidyr::spread() with multiple keys and values

I assume this has been asked multiple times but I couldn't find the proper words to find a workable solution. How can I spread() a data frame based on multiple keys for multiple values? A simplified (I have many more columns to spread, but on only…
blazej
  • 1,678
  • 3
  • 19
  • 41
9
votes
1 answer

Best way to add pandas DataFrame column to row

I have to find the best way to create a new Dataframe using existing DataFrame. Look at this link to have full code : jdoodle.com/a/xKP I have this kind of DataFrame : df = pd.DataFrame({'length': [112, 214, 52,88], 'views': [10000, 50000,…
JC Guidicelli
  • 1,296
  • 7
  • 16
9
votes
1 answer

How do I change a named vector to a data frame retaining the names?

I have following data structure after executing a function: A B C D E 92.08 90.68 54.09 92.87 97.40 F G H I J ... 24.52 67.24 15.63 …
Christopher
  • 2,120
  • 7
  • 31
  • 58
9
votes
2 answers

Getting error: Cannot reshape array of size 122304 into shape (52,28,28)

I'm trying to reshape a numpy array as: data3 = data3.reshape((data3.shape[0], 28, 28)) where data3 is: [[54 68 66 ..., 83 72 58] [63 63 63 ..., 51 51 51] [41 45 80 ..., 44 46 81] ..., [58 60 61 ..., 75 75 81] [56 58 59 ..., 72 75 80] [ 4 4…
akrama81
  • 341
  • 1
  • 7
  • 18