Dear All, I have a matrix with data that is not organised. I would like to go through this and extract it. Each feature has 2 vectors which express the data. I also have an index of the places where the data should be cut. eg.>class(cc)"matrix">cc[,1] [,2] [1,] 1 26 [2,] 2 27 [3,] 3 28 [4,] 4 29 [5,] 5 30 [6,] 6 31 [7,] 7 32 [8,] 8 33 [9,] 9 34 [10,] 1 27 [11,] 1 28 [12,] 2 30 [13,] 3 34 ect......> index[1] "10" "40" Is there a way to take cc[i:index[i-1],] to another format as to where each block could be worked on separately. ie so in one block would be rows1:10 the next block would be rows11:40 and so on. Thanks, Paul -- Research Technician Mass Spectrometry o The / o Scripps \ o Research / o Institute
This will create a list of the matrix subsets: # create a matrix x <- cbind(1:40, runif(40)) index <- c(10,15,33,40) # cut points # create a matrix with start and end points slices <- cbind(start=head(c(1,index + 1), -1), end=index) # create a list with the matrices matrix.subset <- lapply(seq(nrow(slices)), function(.row){ x[slices[.row, 1]:slices[.row, 2], ] }) matrix.subset On 6/18/07, H. Paul Benton <hpbenton@scripps.edu> wrote:> > Dear All, > > > I have a matrix with data that is not organised. I would like to go > through this and extract it. Each feature has 2 vectors which express > the data. I also have an index of the places where the data should be cut. > eg. > >class(cc) > "matrix" > >cc > [,1] [,2] > [1,] 1 26 > [2,] 2 27 > [3,] 3 28 > [4,] 4 29 > [5,] 5 30 > [6,] 6 31 > [7,] 7 32 > [8,] 8 33 > [9,] 9 34 > [10,] 1 27 > [11,] 1 28 > [12,] 2 30 > [13,] 3 34 > ect...... > > index > [1] "10" "40" > > > Is there a way to take cc[i:index[i-1],] to another format as to where > each block could be worked on separately. ie so in one block would be > rows1:10 the next block would be rows11:40 and so on. > > Thanks, > > Paul > > > > -- > Research Technician > Mass Spectrometry > o The > / > o Scripps > \ > o Research > / > o Institute > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Hi Paul, Hope this is what you're looking for: ## reading in text (the first 13 rows of cc from your posting) ## and using smaller indices [(3,8) instead of (10,40)] ## for this example> cc <- "mode<-"(do.call(rbind,+ strsplit(readLines(textConnection(txt))[-1],"[ ]{2,}"))[,-1], + "numeric")> index <- c(3,8)## (1) convert cc to data frame ## (2) split according to factors produced by cut() ## (3) apply data.matrix() to each element of list ## produced by split() to convert back to numeric matrix> s <- lapply(split(as.data.frame(cc),+ f=cut(1:nrow(cc),breaks=c(-Inf,index,Inf))), + data.matrix) ## return result. now s[[1]] contains the first "block", ## s[[2]] contains the second "block", and so on.> s$`(-Inf,3]` V1 V2 1 1 26 2 2 27 3 3 28 $`(3,8]` V1 V2 4 4 29 5 5 30 6 6 31 7 7 32 8 8 33 $`(8, Inf]` V1 V2 9 9 34 10 1 27 11 1 28 12 2 30 13 3 34 --- "H. Paul Benton" <hpbenton at scripps.edu> wrote:> Dear All, > > > I have a matrix with data that is not organised. I would like to go > through this and extract it. Each feature has 2 vectors which express > the data. I also have an index of the places where the data should be cut. > eg. > >class(cc) > "matrix" > >cc > [,1] [,2] > [1,] 1 26 > [2,] 2 27 > [3,] 3 28 > [4,] 4 29 > [5,] 5 30 > [6,] 6 31 > [7,] 7 32 > [8,] 8 33 > [9,] 9 34 > [10,] 1 27 > [11,] 1 28 > [12,] 2 30 > [13,] 3 34 > ect...... > > index > [1] "10" "40" > > > Is there a way to take cc[i:index[i-1],] to another format as to where > each block could be worked on separately. ie so in one block would be > rows1:10 the next block would be rows11:40 and so on. > > Thanks, > > Paul > > > > -- > Research Technician > Mass Spectrometry > o The > / > o Scripps > \ > o Research > / > o Institute > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >