[R] splitting data matrix into submatrices

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Wed Jan 5 08:56:33 CET 2022


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 <faheemjan93 using 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 <jdnewmil using 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 using 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 using 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.



More information about the R-help mailing list