Re: [R] splitting data matrix into submatrices

2022-01-06 Thread Avi Gross via R-help


My apologies. I temporarily used another mailer and I see it did not send my 
message as plain text. I have not yet seen a way to change on a per message 
basis. My code, was naturally displayed badly. Sigh.
rows <- 1826

weekends <- (rows %% 7 <= 1)

satsun <- mymat[weekends,]

workweek <- mymat[!weekends,]

Is this any better?


-Original Message-
From: Avi Gross via R-help 
To: r-help@r-project.org 
Sent: Wed, Jan 5, 2022 12:52 pm
Subject: Re: [R] splitting data matrix into submatrices

Faheem seems to reply to people in private and I replid to him in private but 
seeing later messages, am giving a synopsis in public.
I too suggesed that using a data.frame is a better way to keep his data and use 
it for some purposes.
But my understanding is that his matrix is fairly easy to understand and can be 
indexed given the way it is set up.
Every row of the matrix has info for a single day, with each hour represented 
by one column. There is no date anywhere in sight or any visible index. But the 
collection was done starting on a Sunday and continuing without gaps so every 
seventh following row is a Sunday. So it is trivial to do what he asks using 
the %% operator to select all numbers between 1 and the number of rows in the 
matrix that have a remainder of either 0 (Saturdays) or 1 (Sundays) with 
something like this if the data is in mymat:
rows <- 1826weekends <- (rows %% 7 <= 1)satsun <- mymat[weekends,]
This basically indexes the matrix using a boolean vector. The result can be 
saved as a matrix of weekend data.
Getting the second matrix is as trivial as flipping the boolean vector.
workweek <- mymat[!weekends,]

-Original Message-
From: Faheem Jan 
To: Avi Gross 
Sent: Tue, Jan 4, 2022 11:44 pm
Subject: Re: [R] splitting data matrix into submatrices

First I will thank you for your interest and time. As my first day is Sunday, 
in my data matrix 24 are hourly demand of electricity. As electricity demand is 
different is working and non-working days, so I want a separate analysis for 
working and nonworking days that's why I want to split the data matrix into 
submatrices. Can we use for looping which divides the matrix into submatrices? 

    On Wednesday, January 5, 2022, 09:29:44 AM GMT+5, Avi Gross via R-help 
 wrote:  
 
 
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 
To: R-help Mailing List 
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@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]]

__
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.
  
    [[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.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PL

Re: [R] splitting data matrix into submatrices

2022-01-05 Thread Avi Gross via R-help
Faheem seems to reply to people in private and I replid to him in private but 
seeing later messages, am giving a synopsis in public.
I too suggesed that using a data.frame is a better way to keep his data and use 
it for some purposes.
But my understanding is that his matrix is fairly easy to understand and can be 
indexed given the way it is set up.
Every row of the matrix has info for a single day, with each hour represented 
by one column. There is no date anywhere in sight or any visible index. But the 
collection was done starting on a Sunday and continuing without gaps so every 
seventh following row is a Sunday. So it is trivial to do what he asks using 
the %% operator to select all numbers between 1 and the number of rows in the 
matrix that have a remainder of either 0 (Saturdays) or 1 (Sundays) with 
something like this if the data is in mymat:
rows <- 1826weekends <- (rows %% 7 <= 1)satsun <- mymat[weekends,]
This basically indexes the matrix using a boolean vector. The result can be 
saved as a matrix of weekend data.
Getting the second matrix is as trivial as flipping the boolean vector.
workweek <- mymat[!weekends,]

-Original Message-
From: Faheem Jan 
To: Avi Gross 
Sent: Tue, Jan 4, 2022 11:44 pm
Subject: Re: [R] splitting data matrix into submatrices

First I will thank you for your interest and time. As my first day is Sunday, 
in my data matrix 24 are hourly demand of electricity. As electricity demand is 
different is working and non-working days, so I want a separate analysis for 
working and nonworking days that's why I want to split the data matrix into 
submatrices. Can we use for looping which divides the matrix into submatrices? 

On Wednesday, January 5, 2022, 09:29:44 AM GMT+5, Avi Gross via R-help 
 wrote:  
 
 
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 
To: R-help Mailing List 
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@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]]

__
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.
  
[[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.


Re: [R] splitting data matrix into submatrices

2022-01-05 Thread Stephen H. Dawson, DSL via R-help

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 


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


Re: [R] splitting data matrix into submatrices

2022-01-05 Thread PIKAL Petr
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,]16   11   16
[2,]27   12   17
[3,]38   13   18
[4,]49   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,]16   11   16
[2,]38   13   18
[3,]5   10   15   20
> mat[which(fac==2), ]
 [,1] [,2] [,3] [,4]
[1,]27   12   17
[2,]49   14   19

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Jeff Newmiller
> Sent: Wednesday, January 5, 2022 8:57 AM
> To: Faheem Jan ; R-help 
> 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
>  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
>  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  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.


Re: [R] splitting data matrix into submatrices

2022-01-04 Thread Jeff Newmiller
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  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 
>  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 
> 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.


Re: [R] splitting data matrix into submatrices

2022-01-04 Thread Jeff Newmiller
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  
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.


Re: [R] splitting data matrix into submatrices

2022-01-04 Thread Avi Gross via R-help


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 
To: R-help Mailing List 
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@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]]

__
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] splitting data matrix into submatrices

2022-01-04 Thread Faheem Jan via R-help
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.


[R] Splitting 3D matrix from for loop to generate/save 2D matrices

2010-11-21 Thread Hana Lee
Hi!

I have a matrix called M with dimension (586,100,100). I would like to split
and save this into 586 matrices with dimension 100 by 100.
I have tried the following for loops but couldn't get it work..

l-dim(M)[1]
for (i in (1:l)){
save(M[i,,],file = M_[i].img)
}

Can somebody help me with this? Thanks!

Hana Lee

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Splitting 3D matrix from for loop to generate/save 2D matrices

2010-11-21 Thread Henrik Bengtsson
Hi,

On Sun, Nov 21, 2010 at 9:27 PM, Hana Lee hana...@email.unc.edu wrote:
 Hi!

 I have a matrix called M with dimension (586,100,100).

First of all, In R it is only an object with *two* dimensions that is
called matrix.  Anything with two or more dimensions is called an
array.  Example:

 x - 1:(2*3*4)
 y - matrix(x, ncol=2)
 z - array(x, dim=c(2,3,4))

# VECTORS
 is.vector(x)
[1] TRUE
 is.vector(y)
[1] FALSE
 is.vector(z)
[1] FALSE

# MATRICES
 is.matrix(x)
[1] FALSE
 is.matrix(y)
[1] TRUE
 is.matrix(z)
[1] FALSE

# ARRAYS
 is.array(x)
[1] FALSE
 is.array(y)
[1] TRUE
 is.array(z)
[1] TRUE

So, you've got an *array* (not a matrix).

 I would like to split
 and save this into 586 matrices with dimension 100 by 100.
 I have tried the following for loops but couldn't get it work..

 l-dim(M)[1]
 for (i in (1:l)){
 save(M[i,,],file = M_[i].img)
 }

...but couldn't get it work [as I wanted].

The problem you have is generate a unique filename for matrix.  The
following two lines generate the same filename:

filename - sprintf(M_%d.img, i);
filename - paste(M_, i, .img, sep=);

I prefer to use the sprintf() version.

So,

l - dim(M)[1]
for (i in (1:l)) {
  filename - sprintf(M_%d.img, i);
  save(M[i,,], file=filename);
}

My $.02

/Henrik




 Can somebody help me with this? Thanks!

 Hana Lee

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org 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.


__
R-help@r-project.org 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.


Re: [R] Splitting 3D matrix from for loop to generate/save 2D matrices

2010-11-21 Thread Michael Bedward
Hi Hana,

Use the paste function to create your file names.

for ( i in 1:dim(M)[1] ) save( M[i,,], file=paste(M_, i, .img, sep=) )

Alternatively, use the sprintf function to get names with leading
zeroes for easier sorting of files:

for (i in 1:dim(M)[1] ) save( M[i,,], file=sprintf(M_%03d.img, i) )

Michael


On 22 November 2010 16:27, Hana Lee hana...@email.unc.edu wrote:
 Hi!

 I have a matrix called M with dimension (586,100,100). I would like to split
 and save this into 586 matrices with dimension 100 by 100.
 I have tried the following for loops but couldn't get it work..

 l-dim(M)[1]
 for (i in (1:l)){
 save(M[i,,],file = M_[i].img)
 }

 Can somebody help me with this? Thanks!

 Hana Lee

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org 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.


__
R-help@r-project.org 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.


Re: [R] Splitting 3D matrix from for loop to generate/save 2D matrices

2010-11-21 Thread Henrik Bengtsson
Sorry, I did a mistake corrected below.

On Sun, Nov 21, 2010 at 9:50 PM, hb h...@biostat.ucsf.edu wrote:
 Hi,

 On Sun, Nov 21, 2010 at 9:27 PM, Hana Lee hana...@email.unc.edu wrote:
 Hi!

 I have a matrix called M with dimension (586,100,100).

 First of all, In R it is only an object with *two* dimensions that is called 
 matrix. Anything with two or more dimensions is called an array. Example:

 x - 1:(2*3*4)
 y - matrix(x, ncol=2)
 z - array(x, dim=c(2,3,4))

 # VECTORS
 is.vector(x)
 [1] TRUE
 is.vector(y)
 [1] FALSE
 is.vector(z)
 [1] FALSE

 # MATRICES
 is.matrix(x)
 [1] FALSE
 is.matrix(y)
 [1] TRUE
 is.matrix(z)
 [1] FALSE

 # ARRAYS
 is.array(x)
 [1] FALSE
 is.array(y)
 [1] TRUE
 is.array(z)
 [1] TRUE

 So, you've got an *array* (not a matrix).

 I would like to split
 and save this into 586 matrices with dimension 100 by 100.
 I have tried the following for loops but couldn't get it work..

 l-dim(M)[1]
 for (i in (1:l)){
 save(M[i,,],file = M_[i].img)
 }

 ...but couldn't get it work [as I wanted].

Indeed save(M[i,,], ...) will throw an error.  You also need to subset
to a temporary variable, e.g. X - M[i,,], and save that.  See below.


 The problem you have is generate a unique filename for matrix. The following 
 two lines generate the same filename:

 filename - sprintf(M_%d.img, i);
 filename - paste(M_, i, .img, sep=);

 I prefer to use the sprintf() version.

 So,

 l - dim(M)[1]
 for (i in (1:l)) {
 filename - sprintf(M_%d.img, i);
 save(M[i,,], file=filename);
 }


l - dim(M)[1]
for (i in (1:l)) {
  filename - sprintf(M_%d.img, i);
  X - M[i,,];
  save(X, file=filename);
}


Note that this will load the data into a variable called 'X' when you
load() one of the files.  If you do not want to have to worry about
the name you can use:

library(R.utils);
l - dim(M)[1];
for (i in (1:l)) {
  filename - sprintf(M_%d.img, i);
  saveObject(M[i,,], file=filename);
}

and later load the object into any variable you with:

filename - M_32.img;
Z - loadObject(filename);

/H

 My $.02

 /Henrik




 Can somebody help me with this? Thanks!

 Hana Lee

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org 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.




__
R-help@r-project.org 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.


Re: [R] Splitting 3D matrix from for loop to generate/save 2D matrices

2010-11-21 Thread David Winsemius


On Nov 22, 2010, at 12:27 AM, Hana Lee wrote:


Hi!

I have a matrix called M with dimension (586,100,100).


In R you have an array (not a matrix) wehn the number of dimensions is  
3.



I would like to split
and save this into 586 matrices with dimension 100 by 100.



I have tried the following for loops but couldn't get it work..

l-dim(M)[1]
for (i in (1:l)){
save(M[i,,],


I think the save function needs a name rather than an object for  
evaluation. Also it's not a representation that will be particularly  
useful outside the context of R.



file = M_[i].img)


# the R interpreter is not going to evaluate those i's inside  
quotes, no matter how smart you think it is.



}



Maybe (with some hesitation about the advisability of this):

   l-dim(M)[1]
for (i in (1:l)){
   temp - M[i,,]
   save(temp, file = paste(M_,i,.img, sep=)
}

When these get load()-ed back in, they will each have the have temp,  
so if you read in more than one, only the last one will remain. It  
might make more sense to write them out as a group and read them back  
in the same way.



Can somebody help me with this? Thanks!

Hana Lee

[[alternative HTML version deleted]]

--

David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org 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.


[R] Splitting a matrix

2010-09-16 Thread Megh Dal
Hi, I was trying to split the following matrix dat:

 set.seed(1)
 dat - matrix(rnorm(4*16), 4, 16)
 dat
   [,1]   [,2]   [,3][,4][,5][,6]   
 [,7]   [,8][,9]  [,10]  [,11]
[1,] -0.6264538  0.3295078  0.5757814 -0.62124058 -0.01619026  0.91897737  
0.61982575 -0.4781501  0.38767161 -0.3942900 -0.1645236
[2,]  0.1836433 -0.8204684 -0.3053884 -2.21469989  0.94383621  0.78213630 
-0.05612874  0.4179416 -0.05380504 -0.0593134 -0.2533617
[3,] -0.8356286  0.4874291  1.5117812  1.12493092  0.82122120  0.07456498 
-0.15579551  1.3586796 -1.37705956  1.1000254  0.6969634
[4,]  1.5952808  0.7383247  0.3898432 -0.04493361  0.59390132 -1.98935170 
-1.47075238 -0.1027877 -0.41499456  0.7631757  0.5566632
  [,12]  [,13]  [,14]  [,15]   [,16]
[1,] -0.6887557 -0.1123462  0.3411197 -0.3672215  2.40161776
[2,] -0.7074952  0.8811077 -1.1293631 -1.0441346 -0.03924000
[3,]  0.3645820  0.3981059  1.4330237  0.5697196  0.68973936
[4,]  0.7685329 -0.6120264  1.9803999 -0.1350546  0.02800216

I want to split above one into 4 matrices (probably can be confined into a 
list) wherein 1st matrix consists of columns 1-4, 2nd matrix consists column 
5-8 etc. Here I tried following:

 lapply(split(t(dat), c(4,8,12,16), drop=F), function(x) return(t(matrix(x, 4, 
 4
$`4`
   [,1][,2][,3]   [,4]
[1,] -0.6264538 -0.01619026  0.38767161 -0.1123462
[2,]  0.1836433  0.94383621 -0.05380504  0.8811077
[3,] -0.8356286  0.82122120 -1.37705956  0.3981059
[4,]  1.5952808  0.59390132 -0.41499456 -0.6120264

$`8`
   [,1][,2]   [,3]   [,4]
[1,]  0.3295078  0.91897737 -0.3942900  0.3411197
[2,] -0.8204684  0.78213630 -0.0593134 -1.1293631
[3,]  0.4874291  0.07456498  1.1000254  1.4330237
[4,]  0.7383247 -1.98935170  0.7631757  1.9803999

$`12`
   [,1][,2]   [,3]   [,4]
[1,]  0.5757814  0.61982575 -0.1645236 -0.3672215
[2,] -0.3053884 -0.05612874 -0.2533617 -1.0441346
[3,]  1.5117812 -0.15579551  0.6969634  0.5697196
[4,]  0.3898432 -1.47075238  0.5566632 -0.1350546

$`16`
[,1]   [,2]   [,3][,4]
[1,] -0.62124058 -0.4781501 -0.6887557  2.40161776
[2,] -2.21469989  0.4179416 -0.7074952 -0.03924000
[3,]  1.12493092  1.3586796  0.3645820  0.68973936
[4,] -0.04493361 -0.1027877  0.7685329  0.02800216

However this is not that which I wanted to get. Any idea on the proper code to 
doing that?

Thanks

__
R-help@r-project.org 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.


Re: [R] Splitting a matrix

2010-09-16 Thread Bill.Venables
Here is one possibility:

 set.seed(1)
 dat - matrix(rnorm(4*16), 4, 16)
 dim(dat) - c(4,4,4)
 dat
, , 1

   [,1]   [,2]   [,3][,4]
[1,] -0.6264538  0.3295078  0.5757814 -0.62124058
[2,]  0.1836433 -0.8204684 -0.3053884 -2.21469989
[3,] -0.8356286  0.4874291  1.5117812  1.12493092
[4,]  1.5952808  0.7383247  0.3898432 -0.04493361

, , 2

[,1][,2][,3]   [,4]
[1,] -0.01619026  0.91897737  0.61982575 -0.4781501
[2,]  0.94383621  0.78213630 -0.05612874  0.4179416
[3,]  0.82122120  0.07456498 -0.15579551  1.3586796
[4,]  0.59390132 -1.98935170 -1.47075238 -0.1027877

, , 3

[,1]   [,2]   [,3]   [,4]
[1,]  0.38767161 -0.3942900 -0.1645236 -0.6887557
[2,] -0.05380504 -0.0593134 -0.2533617 -0.7074952
[3,] -1.37705956  1.1000254  0.6969634  0.3645820
[4,] -0.41499456  0.7631757  0.5566632  0.7685329

, , 4

   [,1]   [,2]   [,3][,4]
[1,] -0.1123462  0.3411197 -0.3672215  2.40161776
[2,]  0.8811077 -1.1293631 -1.0441346 -0.03924000
[3,]  0.3981059  1.4330237  0.5697196  0.68973936
[4,] -0.6120264  1.9803999 -0.1350546  0.02800216

  

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Megh Dal
Sent: Thursday, 16 September 2010 5:43 PM
To: r-h...@stat.math.ethz.ch
Subject: [R] Splitting a matrix

Hi, I was trying to split the following matrix dat:

 set.seed(1)
 dat - matrix(rnorm(4*16), 4, 16)
 dat
   [,1]   [,2]   [,3][,4][,5][,6]   
 [,7]   [,8][,9]  [,10]  [,11]
[1,] -0.6264538  0.3295078  0.5757814 -0.62124058 -0.01619026  0.91897737  
0.61982575 -0.4781501  0.38767161 -0.3942900 -0.1645236
[2,]  0.1836433 -0.8204684 -0.3053884 -2.21469989  0.94383621  0.78213630 
-0.05612874  0.4179416 -0.05380504 -0.0593134 -0.2533617
[3,] -0.8356286  0.4874291  1.5117812  1.12493092  0.82122120  0.07456498 
-0.15579551  1.3586796 -1.37705956  1.1000254  0.6969634
[4,]  1.5952808  0.7383247  0.3898432 -0.04493361  0.59390132 -1.98935170 
-1.47075238 -0.1027877 -0.41499456  0.7631757  0.5566632
  [,12]  [,13]  [,14]  [,15]   [,16]
[1,] -0.6887557 -0.1123462  0.3411197 -0.3672215  2.40161776
[2,] -0.7074952  0.8811077 -1.1293631 -1.0441346 -0.03924000
[3,]  0.3645820  0.3981059  1.4330237  0.5697196  0.68973936
[4,]  0.7685329 -0.6120264  1.9803999 -0.1350546  0.02800216

I want to split above one into 4 matrices (probably can be confined into a 
list) wherein 1st matrix consists of columns 1-4, 2nd matrix consists column 
5-8 etc. Here I tried following:

 lapply(split(t(dat), c(4,8,12,16), drop=F), function(x) return(t(matrix(x, 4, 
 4
$`4`
   [,1][,2][,3]   [,4]
[1,] -0.6264538 -0.01619026  0.38767161 -0.1123462
[2,]  0.1836433  0.94383621 -0.05380504  0.8811077
[3,] -0.8356286  0.82122120 -1.37705956  0.3981059
[4,]  1.5952808  0.59390132 -0.41499456 -0.6120264

$`8`
   [,1][,2]   [,3]   [,4]
[1,]  0.3295078  0.91897737 -0.3942900  0.3411197
[2,] -0.8204684  0.78213630 -0.0593134 -1.1293631
[3,]  0.4874291  0.07456498  1.1000254  1.4330237
[4,]  0.7383247 -1.98935170  0.7631757  1.9803999

$`12`
   [,1][,2]   [,3]   [,4]
[1,]  0.5757814  0.61982575 -0.1645236 -0.3672215
[2,] -0.3053884 -0.05612874 -0.2533617 -1.0441346
[3,]  1.5117812 -0.15579551  0.6969634  0.5697196
[4,]  0.3898432 -1.47075238  0.5566632 -0.1350546

$`16`
[,1]   [,2]   [,3][,4]
[1,] -0.62124058 -0.4781501 -0.6887557  2.40161776
[2,] -2.21469989  0.4179416 -0.7074952 -0.03924000
[3,]  1.12493092  1.3586796  0.3645820  0.68973936
[4,] -0.04493361 -0.1027877  0.7685329  0.02800216

However this is not that which I wanted to get. Any idea on the proper code to 
doing that?

Thanks

__
R-help@r-project.org 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.

__
R-help@r-project.org 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.