[R] splitting data matrix into submatrices

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Wed Jan 5 07:10:32 CET 2022

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 )

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
>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