reshape
How do I resize a matrix in MATLAB?
reshape is of course the proper solution, as stated by @gnovice. A nice feature of reshape is that it allows this: A = 1:12; B = reshape(A,4,[]); B = 1 5 9 2 6 10 3 7 11 4 8 12 So if you don’t know how many columns there will be, reshape will compute … Read more
Opposite of melt in python pandas
there are a few ways: using .pivot: >>> origin.pivot(index=’label’, columns=”type”)[‘value’] type a b c label x 1 2 3 y 4 5 6 z 7 8 9 [3 rows x 3 columns] using pivot_table: >>> origin.pivot_table(values=”value”, index=’label’, columns=”type”) value type a b c label x 1 2 3 y 4 5 6 z 7 8 … Read more
What methods can we use to reshape VERY large data sets?
If your real data is as regular as your sample data we can be quite efficient by noticing that reshaping a matrix is really just changing its dim attribute. 1st on very small data library(data.table) library(microbenchmark) library(tidyr) matrix_spread <- function(df1, key, value){ unique_ids <- unique(df1[[key]]) mat <- matrix( df1[[value]], ncol= length(unique_ids),byrow = TRUE) df2 <- … Read more
Reshape an array in NumPy
a = np.arange(18).reshape(9,2) b = a.reshape(3,3,2).swapaxes(0,2) # a: array([[ 0, 1], [ 2, 3], [ 4, 5], [ 6, 7], [ 8, 9], [10, 11], [12, 13], [14, 15], [16, 17]]) # b: array([[[ 0, 6, 12], [ 2, 8, 14], [ 4, 10, 16]], [[ 1, 7, 13], [ 3, 9, 15], [ 5, … Read more
Compute mean and standard deviation by group for multiple variables in a data.frame
This is an aggregation problem, not a reshaping problem as the question originally suggested — we wish to aggregate each column into a mean and standard deviation by ID. There are many packages that handle such problems. In the base of R it can be done using aggregate like this (assuming DF is the input … Read more
Subsetting R data frame results in mysterious NA rows
Wrap the condition in which: df[which(df$number1 < df$number2), ] How it works: It returns the row numbers where the condition matches (where the condition is TRUE) and subsets the data frame on those rows accordingly. Say that: which(df$number1 < df$number2) returns row numbers 1, 2, 3, 4 and 5. As such, writing: df[which(df$number1 < df$number2), … Read more