Hi

As Jeff said for data frames you can use split.
If you insist to work with matrices, the principle is similar but you cannot 
use split.

mat <- matrix(1:20, 5,4)
mat
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

# factor for splitting
fac <- factor(c(1,2,1,2,1))
> fac
[1] 1 2 1 2 1
Levels: 1 2

#splitting matrix according to the factor levels
> mat[which(fac==1), ]
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    3    8   13   18
[3,]    5   10   15   20
> mat[which(fac==2), ]
     [,1] [,2] [,3] [,4]
[1,]    2    7   12   17
[2,]    4    9   14   19

Cheers
Petr

> -----Original Message-----
> From: R-help <r-help-boun...@r-project.org> On Behalf Of Jeff Newmiller
> Sent: Wednesday, January 5, 2022 8:57 AM
> To: Faheem Jan <faheemja...@yahoo.com>; R-help <r-help@r-project.org>
> Subject: Re: [R] splitting data matrix into submatrices
>
> Please reply all so the mailing list is included in the discussion. I don't 
> do 1:1
> tutoring and others can chime in if I make a mistake.
>
> I would say you don't understand what my example did, since it doesn't care
> how many columns are in your data frame. If you are in fact working with a
> matrix, then convert it to a data frame.
>
> As for continuing to help you... you need to provide a minimal reproducible
> example with a small sample data set if what I showed you isn't helping.
>
> [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-
> reproducible-example
>
> [2] http://adv-r.had.co.nz/Reproducibility.html
>
> On January 4, 2022 10:44:39 PM PST, Faheem Jan
> <faheemja...@yahoo.com> wrote:
> >I understand what you have done, it can easily apply in the case of vector
> information mean each day having a single observation. But in my case I have
> 24 observations each day, I want to convert the matrix into two submatrices
> for weekdays and the other for weekends. So please suggest to me anyway
> that how I do this?
> >
> >    On Wednesday, January 5, 2022, 11:10:34 AM GMT+5, Jeff Newmiller
> <jdnew...@dcn.davis.ca.us> wrote:
> >
> > A lot of new R users fail to grasp what makes data frames more useful than
> matrices, or use data frames without even realizing they are not using
> matrices.
> >
> >This is important because there are more tools for manipulating data frames
> than matrices. One tool is the split function... if you have a vector of 
> values
> identifying how each row should be identified you can give that to the split
> function with your data frame and it will return a list of data frames (2 in 
> this
> case).
> >
> >v <- rep( 0:6, length=1826 )
> >wkv <- ifelse( v < 5, "Weekday", "Weekend" ) ans <- split( DF, wkv )
> >ans$Weekday ans$Weekend
> >
> >Note that this is a fragile technique for generating wkv though... usually
> there will be a column of dates that can be used to generate wkv more
> consistently if your data changes.
> >
> >Please read the Posting Guide... using formatted email can cause readers to
> not see what you sent. Use plain text email format... it is a setting in 
> your
> email client.
> >
> >On January 4, 2022 7:52:34 PM PST, Faheem Jan via R-help <r-help@r-
> project.org> wrote:
> >>I have data in a matrix form of order 1826*24 where 1826 represents the
> days and 24 hourly observations on each data. My objective is to split the
> matrix into working (Monday to Friday) and non-working (Saturday and
> Sunday) submatrices. Can anyone help me that how I will do that splitting
> using R?
> >>
> >>
> >>    [[alternative HTML version deleted]]
> >>
> >>______________________________________________
> >>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>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.
> >
>
> --
> Sent from my phone. Please excuse my brevity.
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to