FAQ Database Discussion Community


Matlab reshape back into original image

matlab,matrix,multidimensional-array,vectorization,reshape
I'm trying to reshape a multidimensional array into the original image. I have split an image of 512x512 pixels in sub-matrices of 8x8 pixels using the great solution that I found in this question: sub_images = permute(reshape(permute(reshape(i_image, size(i_image, 1), n, []), [2 1 3]), n, m, []), [2 1 3]);...

Error in reshape(): duplicate 'row names' are not allowed

r,reshape
I have wide longitudinal data that I would like to reshape into long data. This is a sample: sex group id sex.1 group.1 status1 beg1 end1 status2 beg2 end2 1 1000 1 a 1000 1 a Vocational <NA> S2007 HE S2007 S2008 2 1001 1 a 1001 1 a Vocational...

R: automatically copy large amounts of data

r,reshape
I'm new in R, I have some data like this: Yes No age color place 12 5 12-18 red right 2 33 19-30 yellow left ... I need to create new database, answer age color place Yes 12-18 red right Yes 12-18 red right ... (12 times) No 12-18 red...

subset data frame in new data frame using column values

r,dplyr,reshape
I am trying to subset a big data.frame in different data.frames automatically. I have something like: Type1 Type2 Type3 Info1 Info2 Info3 A 1 Z a a a A 2 Y b b b B 4 X c c c A 1 Z d d d I want to create...

strsplit with non-character data

r,reshape,reshape2,strsplit,agrep
1I want to do a strsplit on one variable ID1 to split into ID1_s1 and ID1_s2 and I need to get rid of the strings that are in brackets. # dummy data df1 <- data.frame(ID1=c("Gindalinc","Xaviertechnolgies","anine.inc(Nasq)","Xyzinc"), y=1:4) strsplit(df1$ID1, "\\(") How can i do a strplit to separate the ID1 based into...

Reshape from wide to long format/structure

r,reshape
I have a data frame with 302 obs. of 942 variables. It looks like this : [Actr225009] [Actr225423] [Actr229853] [Actr78542] [Actr225009] 0 NA 3 NA [Actr225423] NA 0 1 5 [Actr229853] 8 NA 0 2 ... And in need to restructure it for each ligne : Actr225009 Actr225009 0 Actr225009...

Matlab audioread doubles instead of singles

matlab,audio,reshape,sampling
I have the following code at the moment: [y,fs] = audioread('some_audio_file.wav'); y is a <8131546 x 2 double>. Why is that? How come the samples are not a 1 x 8131546? Also when I reshape y to get columns of 30 milliseconds I get a <1324 x 6146 double>? Can...

colapse multiple columns values into 1 factor

r,reshape,tidyr
Is there a function to collapse several columns values into 1 factor? Every record has exactly 1 TRUE value for columns 2:4. The resulting value for a record should be the column's name which has the true value. input data frame: data <- data.frame(user=c(1,2,3,4), blue=c(T,F,T,F), green=c(F,F,F,T), red=c(F,T,F,F)) user blue green...

converting a dataframe to a 2 column dataframe based on vaues

r,reshape
I am trying to convert a multi-column dataframe to a 2 column dataframe. Each row is a person (based on id in first col) with multiple attributes. How can I list each person and attribute in 2 columns? Example: temp<-data.frame(V1 = c(1,2,3),V2 = c(4,5,6)) row.names(temp)<-c("person1","person2","person3") temp V1 V2 person1 1...

Parsing messy dataframes to reshaped data

r,reshape
I have a df that looks like this: . <- c("AXX 101", "", "Introduction to AXX", " ", "Prereq: BXX102, BXX101, not open to CXX program", "Antireq: BXX103", "", "AXX 102","AXX Part II", "", "Antireq: BXX101", "", " ") df <- data.frame(.) df . 1 AXX 101 2 3 Introduction...

Melting data with several groups of column names in R

r,reshape,melt
I am working some data with repeated measurements for multiple covariates. The sample data format is like this set.seed(123) df <- data.frame(id = 1001:1003, matrix(rnorm(36),3,12), d=runif(3), e=runif(3), f=runif(3)) colnames(df) <- c('df', paste('a', 1:4, sep=''), paste('b', 1:4, sep=''), paste('c',1:4,sep=''), 'd', 'e', 'f') df id a1 a2 a3 a4 b1 b2 b3...

Select submatrix and vectorize in one command in MATLAB

arrays,matlab,vectorization,reshape,submatrix
Lets say we have an Array A = ones(2, 2, 2) and another matrix P = rand(4). I am wondering if it is possible to write the code temp = A(:, :, 1); X = P * temp(:); into one line of code to save the memory consumed by temp....

transform a dataframe from long to wide in r, but needs date transformation

r,long-integer,reshape
I have a dataframe like this (each "NUMBER" indicate a student): NUMBER Gender Grade Date.Tested WI WR WZ 1 F 4 2014-02-18 6 9 10 1 F 3 2014-05-30 9 8 2 2 M 5 2013-05-02 7 9 15 2 M 4 2009-05-21 5 7 2 2 M 5 2010-04-29...

Formatting long-form survey data in R

r,table,formatting,reshape,long-form
We asked 3 people two or three yes-no questions. Let me denote these 3 people by 101,102,103 the questions by "A", "B","C" and the responses by 0, 1. The result is q<-data.frame(response=c(0,0,1,0,0,1,1), qstn=c("A","B","A","B","A","B","C"), person=c(101,101,102,102,103,103,103)) We need to convert this table to the following format person|qustionA|questionB|questionC 101 | 0 | 0...

Fastest R equivalent to MATLAB's reshape() method?

r,performance,matlab,vectorization,reshape
I am converting a MATLAB script into R and regretting it so far, as it is slower at the moment. I'm trying to use "vectorized functions" as much as possible, but I'm relatively new to R and do not know what is meant by this. From my research for loops...

How exactly work?

python,numpy,random,reshape
When I run this, code it works as expected and gives me 10 images with dimensions of 100x100: import numpy as np for i in range(10): im = np.random.random_integers(0, 255, 10000).reshape((100, 100)) misc.imsave('random_%03d.png' % i, im) But if I change the arguments to reshape to (200, 200), I recieve an...

Reshaping after Interpolation

python,numpy,grid,scipy,reshape
After interpolating data to a target grid i am not able to reshape my data to to match the original shape. The original shape of my data is 900x900 being rows x columns. After the interpolation i have an 1-D array of interpolated values in the new size of the...

Split dataframe on filtered character and make multiple new columns

r,data.frame,reshape
I have a a data preprocessing issue that is very common in my work. I usually have two files which I in the end want to do a large matching operation for. It´s usually a two step process where the first step involves making a "cleaned" dataframe of the first...

Finding paired events with reshape

r,reshape,reshape2
I have a list of users and items that have been bought by them at certain times, and I want to generate a list of those pairs from the raw data. While I can and probably will write a small python script to do it, I have the nagging feeling...

reshaping data frame r

r,data.frame,reshape
Simple reshaping, I have the following data: df<-data.frame(Product=c("A","A","A","B","B","C"), Ingredients=c("Chocolate","Vanilla","Berry","Chocolate","Berry2","Vanilla")) df Product Ingredients 1 A Chocolate 2 A Vanilla 3 A Berry 4 B Chocolate 5 B Berry2 6 C Vanilla I want a column for each unique value of "ingredient", for example: df2 Product Ingredient_1 Ingredient_2 Ingredient_3 A Chocolate Vanilla...

Reshape a row to collumns based in condition data frame R

r,table,data.frame,reshape
I have this data frame in R: x 1 [email protected] 2 [email protected] 3 43 4 [email protected] 5 [email protected] 6 13 7 [email protected] 8 [email protected] 9 31 10 [email protected] 11 [email protected] 12 32 I would like to have a data frame with 3 columns, not just one: x y value 1...

Flatten list column in data frame with ID column

r,data.frame,reshape
My data frame contains the output of a survey with a select multiple question type. Some cells have multiple values. df <- data.frame(a=1:3,b=I(list(1,1:2,1:3))) df a b 1 1 1 2 2 1, 2 3 3 1, 2, 3 I would like to flatten out the list to obtain the following...

reshape data with non-unique id and varying time frames

r,reshape,reshape2,melt
I have a dataset with the following format: name1 year name2 profits2010 profits2009 count AA 2009 AA 10 15 20 AA 2010 AA 10 15 3 BB 2009 BB 4 NA 34 BB 2010 BB 4 NA 4 I need to reshape the data to this format.Any ideas on how...

How to reshape numpy image? [duplicate]

python,image,numpy,reshape
This question already has an answer here: How to read image from numpy array into PIL Image? 2 answers I have an image as a numpy array with shape (channels, height, width) how can I reshape it so that it has shape (height, width, channels)?...

How to expand data before reshape in R

r,reshape,expand
I have a dataframe that looks like this: as.is <- data.frame(Project = c('Proj A', 'Proj B', 'Proj C', 'Proj D'), Start.Date = c('16.02.2015', '02.03.2015', '16.02.2015', '09.03.2015'), Duration = c(3, 2, 2, 4), No.Of.Resources = c(3, 5, 2, 6)) I need to change the format so it looks like this: to.be...

Stata - panel data from column data

panel,stata,reshape
Panel data newbie here! I have data in Stata in the following format: Name Company1 Company2 Company3 Company4 Company5 Company6 1985 6.0781 2.4766 1.4258 2.6508 13.2083 1986 6.4844 3.0938 2.1953 3.1351 15.7917 1987 10.1563 .2769 5.7109 3.6406 4.4058 15.5833 1988 10.4688 .4219 5.125 3.75 3.6767 8.1667 1989 11.0625 .4289 5.4453...

Reshaping data in R using melt

r,reshape,melt
I have tried to apply what people have suggested to reshape data, but somehow I am failing to get my data reshaped like I want it. I would really appreciate it if someone can help: My data looks like this: AB LP PD1 PD2 PY1 PY2 PY3 PY4 PY5 t...

Counting specific rows in a sequence depending on function

r,filter,dplyr,reshape
Let's say I have a data frame consisting of a number of rows sequenced, like this: mag_numbers <- c(rep(seq(1,-1,length.out = 601),times=3), rep(seq(1,0.98,length.out = 601),times=2)) time <- c(rep(seq(90,54144,length.out = 601),times=2), rep(seq(90,49850,length.out = 601),times=3)) data = data.frame(time=rep(time,times=6), mag=rep(mag_numbers, times=6)) data time mag 1 90.00 1.0000000 2 180.09 0.9966667 3 270.18 0.9933333 4...

Speedy/elegant way to unite many pairs of columns

r,reshape,dplyr,tidyr
Is there an elegant/fastR way to combine all pairs of columns in a data.frame? For example, using mapply() and paste() we can turn this data.frame: mydf <- data.frame(a.1 = letters, a.2 = 26:1, b.1 = letters, b.2 = 1:26) head(mydf) a.1 a.2 b.1 b.2 1 a 26 a 1 2...

stacking 2D matrix using Python

python-2.7,matrix,reshape
I have a script that reshapes a 1024x1024 matrix into 32x32 matrices. Here it is the code: import numpy as np filename = r'bb1e03' background = r'bb1e03_background' size = 1024 resize = 32 n = np.arange(size) T0 = np.loadtxt(filename, unpack=False) bg = np.loadtxt(background, unpack=False) T = T0-bg for s in...

Reshape column values to column names

r,reshape
I've got a dataset with the following structure: df <- data.frame(mult=c(1,2,3,4),red=c(1,0.9,0.8,0.7), result=c('value1','value2','value3','value4')) that I'd like to display in a 3-D plot (x axis: mult, y axis: red, and the x-y points would be 'result') or multiple 2-D plots. Obviously the real DF has a lot more rows and combinations of...

Error when I used R to Convert basket data format to single format

r,data.frame,reshape
I want to convert a basket format to single,and used the code by "Convert basket to single", but an error happened as below: > Data <- read.table("r9.txt") > Data V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 1 A3322 neutral 157 158 159 160 161 162 163...

Convert a long format to wide

r,function,reshape,reshape2
My data frame looks like this: x <- data.frame(c("a","a","a","a","b","b","c","c","c","a", "a"), c(1,2,3,4,1,2,1,2,3, 1, 2)) names(x) <- c("id","nr") id nr 1 a 1 2 a 2 3 a 3 4 a 4 5 b 1 6 b 2 7 c 1 8 c 2 9 c 3 10 a 1 11 a...

Creating factor-level indicator variables in R using spread or cast

r,reshape,dplyr,factors,tidyr
Assume a data structure like the following MemberID <- c(123,123,234,234) nbin <- 4 imatrix <- matrix(sample(c(0,1), size=nbin * length(MemberID), replace=TRUE), nrow=length(MemberID)) colnames(imatrix) <- letters[1:nbin] years <- c("Y1","Y2","Y1","Y2") mydf <- data.frame(cbind(MemberID, years, imatrix)) How can I make a similar data structure such that I have an indicator for each level of...

Manipulating Data Frame in R by ID

r,data.frame,reshape
I have a data frame: ID date term estimate unit1 1/1/2015 intercept 1.01 unit1 1/1/2015 x1 2.01 unit1 1/1/2015 x2 3.01 unit1 1/1/2015 x3 4.01 unit1 1/1/2015 x4 5.01 unit2 1/1/2015 intercept 1.01 unit2 1/1/2015 x1 -1.01 unit2 1/1/2015 x2 1.01 unit2 1/1/2015 x3 2.01 unit1 1/2/2015 intercept 1.01 unit1...

Reshape column vector

matlab,matrix,vector,reshape
Hello I'm working with MATLAB and I have a "z" column vector that has dimension of (9680 x 1). I want to reshape it in order to have an array "z" of dimension (44 x 220). I'm doing the following: z=reshape(z,44,220); I also tried: z=reshape(z,[44,220]); But the output is not...

Reshape from long to wide format

r,reshape
article_code customer_code des_subsettore des_subcat 9014 8.92E+12 GROCERY CONDIMENTI PRIMI (PELATI & SUGHI) 9014 8.92E+12 GROCERY CONDIMENTI PRIMI (PELATI & SUGHI) 9014 8.92E+12 GROCERY articlo 9014 3.25E+12 GROCERY articlo 9014 3.25E+12 GROCERY CONDIMENTI PRIMI (PELATI & SUGHI) 9014 3.25E+12 GROCERY CONDIMENTI PRIMI (PELATI & SUGHI) output required customer_code des_subcat des_subcat1 8.92E+12...

reshape matrix in matlab

matlab,matrix,reshape
I don't see the bug anymore...maybe (very probably :-) ) there's even a much more easier and faster way of doing it... I summarized the important columns of my huge data frame in a little expData (see below). The problem is actually quite easy, but I'm just blind for the...

Reshaping cell array in matlab - k*MxN to M x k*N

matlab,reshape,cell-array
I have a k * MxN cell array that I want to reshape into a M x k * N array. But I want to do this blockwise so that the row structure is preserved. A B C --> A B C G H I M N O D E...

Numpy reshape on view

python,numpy,reshape
I'm confused about the results of numpy reshape operated on a view. In the following q.flags shows that it does not own the data, but q.base is neither x nor y, so what is it? I'm surprised to see that q.strides is 8 which means that it gets the next...

Rearranging information from data frame in R

r,reshape
I have the following df, which was obtained from a excel file: df1 <- data.frame( Colour = c("Green","Red","Blue"), Code = c("N","U", "U"), User1 = c("John","Brad","Peter"), User2 = c("Meg","Meg","John"), User3= c("", "Lucy", "")) I need to rearrange it in order to obtain a data frame where all names are listed in...

r reshape data using row with NA to identify new column

r,data.frame,reshape
I have a dataset in R that looks like this: DF <- data.frame(name=c("A","b","c","d","B","e","f"), x=c(NA,1,2,3,NA,4,5)) I would like to reshape it into: rDF <- data.frame(name=c("b","c","d","e","f"), x=c(1,2,3,4,5), head=c("A","A","A","B","B")) where the first row with an NA identifies a new column, and takes that "row value" until the next row with an NA, and...

How to reformat an R data frame with multiple rows into one row

r,reshape
I have data frames like the following that I need to reformat into a single row, so that I can create a new data frame that's a collection of many of the simpler data frames, with one row in the new data frame representing all of the data of one...

Mutate to obtain values before and after a value

r,plyr,reshape
I have a a data set with a format similar to: amount | event ------ | ------ 3 | FALSE 4 | FALSE 6 | TRUE 7 | FALSE 3 | FALSE 4 | TRUE 8 | FALSE And want to split and mutate based on the values of the...

How to reshape 4d array to 2d array in numpy

python-2.7,numpy,reshape
I have a 4d array of shape like this. It has total 18*100 = 1800 rows and 30 dimensional outputs per row (18, 100, 30, 1, 1) i want to convert or reshape this into 2d array, the easiest way (1800,30) Sorry for being so naive with numpy, but please...

How can I avoid a stack overflow when Fortran RESHAPE produces a large, internal, temporary array?

stack,fortran,heap,stack-overflow,reshape
I have some Fortran code that calls RESHAPE to reorder a matrix such that the dimension that I am now about to loop over becomes the first varying dimension (Column-major order in Fortran). This has nothing to do with C/Fortran interoperability. Now the matrix is rather large and when I...

Reshape start-end time intervals to smaller intervals in R

r,apply,reshape,seq
Here is duration data by time intervals. id <- c("A", "B", "B", "B", "C", "C", "D", "E", "F", "F", "F", "F") start <- c(368, 200, 230, 788, 230, 521, 272, 306, 0, 162, 337, 479) end <- c(373.98, 229.98, 233.98, 842.98, 239.98, 639.98, 285.98, 306.98, 95.98, 162.98, 339.98, 539.98) value...

Reshape a data frame

r,data.frame,reshape
I have the following data frame structure(list(X1 = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), V1 = c(1, NA, 1, 0, NA, NA, NA, NA, NA, NA, NA, NA), V2 = c(NA, NA, NA, NA, 0, 0.25,...

Convert from n x m matrix to long matrix in R

r,matrix,reshape,reshape2
Note: This is not a graph question. I have an n x m matrix: > m = matrix(1:6,2,3) > m a b c d 1 2 3 e 4 5 6 I would like to convert this to a long matrix: > m.l a d 1 a e 4 b...

data cleaning and matching names in r

r,matching,reshape,reshape2
df1 <- structure(list(Name1 = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 12L, 12L, 13L, 14L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 17L, 18L, 19L, 19L,...

inverse reshape in matlab

matlab,reshape
I have used the function reshape() to help change sizes from a 3D matrix to a 2D one. Eg : a = [1 2; 3 4]; b = [5 6; 7 8]; c = cat(3,a,b); Here c is the matrix with 3 dimensions. I used reshape to change it thus:...

Re-shape pandas dataframe stack/unstack

python,pandas,reshape,melt
df = pd.DataFrame({'BORDER':['GERMANY','FRANCE','ITALY','USA','CANADA','MEXICO','INDIA','CHINA','JAPAN' ], 'ASID':[21, 32, 99, 77,66,55,44,88,111], 'HOUR1':[2 ,2 ,2 ,4 ,4 ,4 ,6 ,6, 6],'HOUR2':[3 ,3 ,3, 5 ,5 ,5, 7, 7, 7], 'HOUR3':[8 ,8 ,8, 12 ,12 ,12, 99, 99, 99], 'PRICE1':[2 ,2 ,2 ,4 ,4 ,4 ,6 ,6, 6], 'PRICE2':[2 ,2 ,2 ,4 ,4 ,4 ,6...

How to loop dcast function in reshape package in R

r,loops,reshape,lapply
Being a relatively new R user, I have trouble with any looping functions. I have looked at many tutorials but the examples in them are usually very basic and therefore easy to execute. However I need create slightly more complex loops and am having a lot of trouble figuring out...

reshape wide to long using prefix as id in R

r,reshape
In R, Is it possible to use the reshape function to reshape a data-frame from wide to long with keeping the prefix of the name as the id and the suffix as the column names? For example, I might have a long data-frame like this: log_a_mean | b_mean | c_mean...

Numpy reshape array of arrays to 1D

python,arrays,numpy,reshape
How do I get x to become a 1D array? I found it convenient to create x like this, x=np.array([[0,-1,0]*12,[-1,0,0]*4]) print x print len(x) returns array([ [0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,...

Reshaping large dataset in R

r,reshape,reshape2
I am trying to reshape a large dataset and have a problem not getting the results in the right order as I want to. Here is what the data looks like: GeoFIPS GeoName IndustryID Description X2001 X2002 X2003 X2004 X2005 10180 Abilene, TX 21 Mining 96002 92407 127138 150449 202926...

How to divide one column in to multiple columns by other variables in R

r,reshape
I have a data set data Choice Length Gender 1 I subadults M 2 F subadults M 3 F subadults M 4 F subadults M 5 I subadults M 6 F subadults M 7 I subadults M 8 F subadults M 9 I subadults M 10 I subadults M 11...