Skip to main content

Questions tagged [reshape]

In R, Matlab, NumPy and APL, reshape functions allow data to be transformed into more convenient forms.

383 votes
15 answers
493k views

How to reshape data from long to wide format

I'm having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name = rep(c("firstName", "secondName"), each=4), numbers = rep(1:4, 2), value = rnorm(8) )...
Steve's user avatar
  • 5,937
251 votes
9 answers
334k views

Reshaping data.frame from wide to long format

I have some trouble to convert my data.frame from a wide table to a long table. At the moment it looks like this: Code Country 1950 1951 1952 1953 1954 AFG Afghanistan 20,249 ...
mropa's user avatar
  • 11.7k
55 votes
8 answers
26k views

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 ...
daj's user avatar
  • 7,113
30 votes
9 answers
9k views

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 ...
Hotamd6's user avatar
  • 315
163 votes
6 answers
130k views

Split delimited strings in a column and insert as new rows [duplicate]

I have a data frame as follow: +-----+-------+ | V1 | V2 | +-----+-------+ | 1 | a,b,c | | 2 | a,c | | 3 | b,d | | 4 | e,f | | . | . | +-----+-------+ Each of the alphabet is ...
Boxuan's user avatar
  • 5,097
151 votes
6 answers
113k views

Reshape three column data frame to matrix ("long" to "wide" format) [duplicate]

I have a data.frame that looks like this. x a 1 x b 2 x c 3 y a 3 y b 3 y c 2 I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like: ...
MalteseUnderdog's user avatar
35 votes
5 answers
52k views

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 = ...
Alex Burdusel's user avatar
117 votes
5 answers
127k views

Gather multiple sets of columns

I have data from an online survey where respondents go through a loop of questions 1-3 times. The survey software (Qualtrics) records this data in multiple columns—that is, Q3.2 in the survey will ...
Andrew's user avatar
  • 38k
32 votes
2 answers
41k views

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 ...
user3315563's user avatar
25 votes
3 answers
10k views

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 ...
kmario23's user avatar
  • 60.1k
-1 votes
1 answer
10k views

Using Reshape from wide to long in R [closed]

I am trying to reshape Data from wide to long in R. my data in wide format looks like this: I have the following Data-Matrix: in the rows i have the different companies, and in the columns in have ...
Gritti's user avatar
  • 85
114 votes
6 answers
220k views

Pandas long to wide reshape, by two variables

I have data in long format and am trying to reshape to wide, but there doesn't seem to be a straightforward way to do this using melt/stack/unstack: Salesman Height product price Knut ...
Luke's user avatar
  • 6,949
1 vote
2 answers
2k views

R Reshape data frame from long to wide format? [duplicate]

What's the best way to convert the data frame below from long to wide format? I tried to use reshape but didn't get the desired results. 2015 PROD A test1 2015 PROD A blue 2015 PROD A 50 ...
Curious 's user avatar
43 votes
5 answers
74k views

Reshaping wide to long with multiple values columns [duplicate]

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 ...
Vasily A's user avatar
  • 8,586
677 votes
12 answers
696k views

What does -1 mean in numpy reshape?

A 2D array can be reshaped into a 1D array using .reshape(-1). For example: >>> a = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> a.reshape(-1) array([[1, 2, 3, 4, 5, 6, 7, 8]]) ...
user2262504's user avatar
  • 7,217

15 30 50 per page
1
2 3 4 5
31