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]]
If you explain better, we can help. But first consider what you are asking and how it is relected in the data. Is the first component a row number whose meaning is day 1 contains a 1 or perhaps 0 and the next row contains one more? Or is there some kind of date in there? What day of the week is the first day? Is it a Sunday or a Wednesday or what? Just for arguments sake, say the first row is a Sunday and all later rows are sequential. Then row 1 is a weekend and row 7 is a weekend and so are rows 7 more (8 and 18) and those 7 more and so on.? What do you call it when you are looking for the remainder after dividing by 7? Can you make a list of indices that contain every possible weekend, and another index containing just the non-weekends?? If other things like Holidays do not matter, you can index the matrix many ways by asking for all columns but only the rows that your logical index wants.? If I have misunderstood the problem, ignore. -----Original Message----- From: Faheem Jan via R-help <r-help at r-project.org> To: R-help Mailing List <r-help at r-project.org> Sent: Tue, Jan 4, 2022 10:52 pm Subject: [R] splitting data matrix into submatrices 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 at 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. [[alternative HTML version deleted]]
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 at 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 at 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.
Hi, Please post the data structure so it is more clear what data is being massaged. Thanks, *Stephen Dawson, DSL* /Executive Strategy Consultant/ Business & Technology +1 (865) 804-3454 http://www.shdawson.com <http://www.shdawson.com> On 1/4/22 10:52 PM, Faheem Jan via R-help 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 at 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. >