Re: [R] Matrix scalar operation that saves memory?

2023-04-13 Thread Richard O'Keefe
"wear your disc quite badly"?
If you can afford a computer with 512 GB of memory,
you can afford to pay $100 for a 2 TB external SSD,
use it as scratch space, and throw it away after a
month of use.  A hard drive is expected to last for
more than 40,000 hours of constant use.  Are you
sure that your own disc is so fragile?  Hard drives
are pretty cheap these days.  You could afford to
pay $50 for a 2 TB external hard drive, use it as
scratch space, and throw it away.

I've been around long enough to remember when the idea
of processing a 1000x1000 matrix in memory was greeted
with hysterical laughter and a recommendation to stop
smoking whatever I was smoking.   (But not old enough
to remember shuffling matrices around on tape.  Shudder.)

If you want to work on two 300GB matrices using a machine
with 512GB of RAM, you are going to be using a disc or
SSD, like it or not.  You can leave it up to the paging
subsystem of your OS, which will do its poor best, or you
can explicitly schedule reads and writes in your program,
and if you use asynchronous I/O it might overlap quite nicely.

Assuming for the sake of arithmetic that your matrix
elements are complex numbers represented as pairs of
double precision floats, that's 16 bytes per element,
or 300e9/16 = 1.975e10 elements = n^2 elements where n = 136,930.
Other than adding, subtracting, and multiplying by a scalar,
there's not much you can do with an nxn matrix that won't take
time proportional to n^3.

Is there any way you can divide the matrix into (possibly
overlapping) blocks and do the work on a cluster?  Or a
block at a time?


On Wed, 12 Apr 2023 at 15:54, Shunran Zhang <
szh...@ngs.gen-info.osaka-u.ac.jp> wrote:

> Thanks for the info.
>
> For the data type, my matrix as of now is indeed a matrix in a perfect
> square shape filled in a previous segment of code, but I believe I could
> extract one row/column at a time to do some processing. I can also
> change that previous part of code to change the data type of it to
> something else if that helps.
>
> Saving it to a file for manipulation and reading it back seems to be
> quite IO intensive - writing 600G of data and reading 300G back from a
> hard drive would make the code extremely heavy as well as wear my disk
> quite badly.
>
> For now I'll try the row-by-row method and hope it works...
>
> Sincerely,
> S. Zhang
>
>
> On 2023/04/12 12:39, avi.e.gr...@gmail.com wrote:
> > The example given does not leave room for even a single copy of your
> matrix
> > so, yes, you need alternatives.
> >
> > Your example was fairly trivial as all you wanted to do is subtract each
> > value from 100 and replace it. Obviously something like squaring a matrix
> > has no trivial way to do without multiple copies out there that won't
> fit.
> >
> > One technique that might work is a nested loop that changes one cell of
> the
> > matrix at a time and in-place. A variant of this might be a singe loop
> that
> > changes a single row (or column) at a time and in place.
> >
> > Another odd concept is to save your matrix in a file with some format you
> > can read back in such as a line or row at a time, and then do the
> > subtraction from 100 and write it back to disk in another file. If you
> need
> > it again, I assume you can read it in but perhaps you should consider
> how to
> > increase some aspects of your "memory".
> >
> > Is your matrix a real matrix type or something like a list of lists or a
> > data.frame? You may do better with some data structures that are more
> > efficient than others.
> >
> > Some OS allow you to use virtual memory that is mapped in and out from
> the
> > disk that allows larger things to be done, albeit often much slower. I
> also
> > note that you can remove some things you are not using and hope garbage
> > collection happens soon enough.
> >
> > -Original Message-
> > From: R-help  On Behalf Of Shunran Zhang
> > Sent: Tuesday, April 11, 2023 10:21 PM
> > To: r-help@r-project.org
> > Subject: [R] Matrix scalar operation that saves memory?
> >
> > Hi all,
> >
> > I am currently working with a quite large matrix that takes 300G of
> > memory. My computer only has 512G of memory. I would need to do a few
> > arithmetic on it with a scalar value. My current code looks like this:
> >
> > mat <- 100 - mat
> >
> > However such code quickly uses up all of the remaining memory and got
> > the R script killed by OOM killer.
> >
> > Are there any more memory-efficient way of doing such operation?
> >
> > Thanks,
> >
> > S. Zhang
> >
> >   [[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 

Re: [R] Matrix scalar operation that saves memory?

2023-04-12 Thread Iago Giné Vázquez
You may take a look at the bigmemory package or other which deal with large 
memory data in 
https://cran.r-project.org/web/views/HighPerformanceComputing.html#large-memory-and-out-of-memory-data
Some extra explanation is in https://stackoverflow.com/a/11127229/997979

Iago


De: R-help  de part de Eric Berger 

Enviat el: dimecres, 12 d’abril de 2023 8:38
Per a: Bert Gunter 
A/c: R-help 
Tema: Re: [R] Matrix scalar operation that saves memory?

One possibility might be to use Rcpp.
An R matrix is stored in contiguous memory, which can be considered as a
vector.
Define a C++ function which operates on a vector in place, as in the
following:

library(Rcpp)
cppFunction(
  'void subtractConst(NumericVector x, double c) {
  for ( int i = 0; i < x.size(); ++i)
x[i] = x[i] - c;
}')

Try this function out on a matrix. Here we define a 5x2 matrix

m <- matrix(150.5 + 1:10, nrow=5)
print(m)

  [,1]  [,2]
[1,] 151.5 156.5
[2,] 152.5 157.5
[3,] 153.5 158.5
[4,] 154.5 159.5
[5,] 155.5 160.5

Now call the C++ function

subtractConst(m,100.0)
print(m)

[,1] [,2]
[1,] 51.5 56.5
[2,] 52.5 57.5
[3,] 53.5 58.5
[4,] 54.5 59.5
[5,] 55.5 60.5

HTH,
Eric


On Wed, Apr 12, 2023 at 7:34 AM Bert Gunter  wrote:

> I doubt that R's basic matrix capabilities can handle this, but have a look
> at the Matrix package, especially if your matrix is some special form.
>
> Bert
>
> On Tue, Apr 11, 2023, 19:21 Shunran Zhang <
> szh...@ngs.gen-info.osaka-u.ac.jp>
> wrote:
>
> > Hi all,
> >
> > I am currently working with a quite large matrix that takes 300G of
> > memory. My computer only has 512G of memory. I would need to do a few
> > arithmetic on it with a scalar value. My current code looks like this:
> >
> > mat <- 100 - mat
> >
> > However such code quickly uses up all of the remaining memory and got
> > the R script killed by OOM killer.
> >
> > Are there any more memory-efficient way of doing such operation?
> >
> > Thanks,
> >
> > S. Zhang
> >
> > [[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
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix scalar operation that saves memory?

2023-04-12 Thread Eric Berger
One possibility might be to use Rcpp.
An R matrix is stored in contiguous memory, which can be considered as a
vector.
Define a C++ function which operates on a vector in place, as in the
following:

library(Rcpp)
cppFunction(
  'void subtractConst(NumericVector x, double c) {
  for ( int i = 0; i < x.size(); ++i)
x[i] = x[i] - c;
}')

Try this function out on a matrix. Here we define a 5x2 matrix

m <- matrix(150.5 + 1:10, nrow=5)
print(m)

  [,1]  [,2]
[1,] 151.5 156.5
[2,] 152.5 157.5
[3,] 153.5 158.5
[4,] 154.5 159.5
[5,] 155.5 160.5

Now call the C++ function

subtractConst(m,100.0)
print(m)

[,1] [,2]
[1,] 51.5 56.5
[2,] 52.5 57.5
[3,] 53.5 58.5
[4,] 54.5 59.5
[5,] 55.5 60.5

HTH,
Eric


On Wed, Apr 12, 2023 at 7:34 AM Bert Gunter  wrote:

> I doubt that R's basic matrix capabilities can handle this, but have a look
> at the Matrix package, especially if your matrix is some special form.
>
> Bert
>
> On Tue, Apr 11, 2023, 19:21 Shunran Zhang <
> szh...@ngs.gen-info.osaka-u.ac.jp>
> wrote:
>
> > Hi all,
> >
> > I am currently working with a quite large matrix that takes 300G of
> > memory. My computer only has 512G of memory. I would need to do a few
> > arithmetic on it with a scalar value. My current code looks like this:
> >
> > mat <- 100 - mat
> >
> > However such code quickly uses up all of the remaining memory and got
> > the R script killed by OOM killer.
> >
> > Are there any more memory-efficient way of doing such operation?
> >
> > Thanks,
> >
> > S. Zhang
> >
> > [[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] Matrix scalar operation that saves memory?

2023-04-11 Thread Bert Gunter
I doubt that R's basic matrix capabilities can handle this, but have a look
at the Matrix package, especially if your matrix is some special form.

Bert

On Tue, Apr 11, 2023, 19:21 Shunran Zhang 
wrote:

> Hi all,
>
> I am currently working with a quite large matrix that takes 300G of
> memory. My computer only has 512G of memory. I would need to do a few
> arithmetic on it with a scalar value. My current code looks like this:
>
> mat <- 100 - mat
>
> However such code quickly uses up all of the remaining memory and got
> the R script killed by OOM killer.
>
> Are there any more memory-efficient way of doing such operation?
>
> Thanks,
>
> S. Zhang
>
> [[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] Matrix scalar operation that saves memory?

2023-04-11 Thread Shunran Zhang

Thanks for the info.

For the data type, my matrix as of now is indeed a matrix in a perfect 
square shape filled in a previous segment of code, but I believe I could 
extract one row/column at a time to do some processing. I can also 
change that previous part of code to change the data type of it to 
something else if that helps.


Saving it to a file for manipulation and reading it back seems to be 
quite IO intensive - writing 600G of data and reading 300G back from a 
hard drive would make the code extremely heavy as well as wear my disk 
quite badly.


For now I'll try the row-by-row method and hope it works...

Sincerely,
S. Zhang


On 2023/04/12 12:39, avi.e.gr...@gmail.com wrote:

The example given does not leave room for even a single copy of your matrix
so, yes, you need alternatives.

Your example was fairly trivial as all you wanted to do is subtract each
value from 100 and replace it. Obviously something like squaring a matrix
has no trivial way to do without multiple copies out there that won't fit.

One technique that might work is a nested loop that changes one cell of the
matrix at a time and in-place. A variant of this might be a singe loop that
changes a single row (or column) at a time and in place.

Another odd concept is to save your matrix in a file with some format you
can read back in such as a line or row at a time, and then do the
subtraction from 100 and write it back to disk in another file. If you need
it again, I assume you can read it in but perhaps you should consider how to
increase some aspects of your "memory".

Is your matrix a real matrix type or something like a list of lists or a
data.frame? You may do better with some data structures that are more
efficient than others.

Some OS allow you to use virtual memory that is mapped in and out from the
disk that allows larger things to be done, albeit often much slower. I also
note that you can remove some things you are not using and hope garbage
collection happens soon enough.

-Original Message-
From: R-help  On Behalf Of Shunran Zhang
Sent: Tuesday, April 11, 2023 10:21 PM
To: r-help@r-project.org
Subject: [R] Matrix scalar operation that saves memory?

Hi all,

I am currently working with a quite large matrix that takes 300G of
memory. My computer only has 512G of memory. I would need to do a few
arithmetic on it with a scalar value. My current code looks like this:

mat <- 100 - mat

However such code quickly uses up all of the remaining memory and got
the R script killed by OOM killer.

Are there any more memory-efficient way of doing such operation?

Thanks,

S. Zhang

[[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] Matrix scalar operation that saves memory?

2023-04-11 Thread avi.e.gross
The example given does not leave room for even a single copy of your matrix
so, yes, you need alternatives.

Your example was fairly trivial as all you wanted to do is subtract each
value from 100 and replace it. Obviously something like squaring a matrix
has no trivial way to do without multiple copies out there that won't fit.

One technique that might work is a nested loop that changes one cell of the
matrix at a time and in-place. A variant of this might be a singe loop that
changes a single row (or column) at a time and in place.

Another odd concept is to save your matrix in a file with some format you
can read back in such as a line or row at a time, and then do the
subtraction from 100 and write it back to disk in another file. If you need
it again, I assume you can read it in but perhaps you should consider how to
increase some aspects of your "memory".

Is your matrix a real matrix type or something like a list of lists or a
data.frame? You may do better with some data structures that are more
efficient than others.

Some OS allow you to use virtual memory that is mapped in and out from the
disk that allows larger things to be done, albeit often much slower. I also
note that you can remove some things you are not using and hope garbage
collection happens soon enough.

-Original Message-
From: R-help  On Behalf Of Shunran Zhang
Sent: Tuesday, April 11, 2023 10:21 PM
To: r-help@r-project.org
Subject: [R] Matrix scalar operation that saves memory?

Hi all,

I am currently working with a quite large matrix that takes 300G of 
memory. My computer only has 512G of memory. I would need to do a few 
arithmetic on it with a scalar value. My current code looks like this:

mat <- 100 - mat

However such code quickly uses up all of the remaining memory and got 
the R script killed by OOM killer.

Are there any more memory-efficient way of doing such operation?

Thanks,

S. Zhang

[[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] Matrix::solve() with 1-d arrays -- treating "array" as "numeric"?

2021-04-19 Thread Martin Maechler
> Deepayan Sarkar 
> on Mon, 19 Apr 2021 09:56:58 +0530 writes:

> On Sat, Apr 17, 2021 at 9:08 PM Martin Maechler
>  wrote:
>> 
>> > Deepayan Sarkar > on Fri, 16 Apr 2021 11:34:20
>> +0530 writes:
>> 
>> > I get what I initially thought was unexpected behaviour
>> from:
>> 
>> > x <- tapply(runif(100), sample(5, 100, TRUE), mean) >
>> solve(Diagonal(5), x) > # Error: not-yet-implemented
>> method for solve(, ).  > # ->> Ask the
>> package authors to implement the missing feature.
>> 
>> ((why did you not ask the package authors ?? --- never
>> mind))
>> 
>> 
>> > This is because x is a 1-D array, so the operation is
>> not > well-defined. Would it make sense for Matrix to
>> support this (treat > 1-D arrays as column vectors, as it
>> does for plain vectors)? Or should > I make my intent
>> clear with
>> 
>> > solve(Diagonal(5), as.vector(x))
>> 
>> well ...
>> 
>> The "fun" thing is that it actually works when Matrix
>> methods are not fully available, i.e., if you do *not* do
>> require(Matrix) or equivalent, but rather only load the
>> Matrix namespace via
>> 
>> solve(Matrix::Diagonal(5), x)
>> 
>> actually currently works correctly by some "good
>> coincidence" (I have not yet tried to understand, as
>> that's a bit painful: selectMethod("solve",
>> c("ddiMatrix", "array")) is "lying" here ! )
>> 
>> However this looks like a more general problem with S4
>> methods -- and probably a good reason for asking on
>> R-help -- namely, the fact that d1-dimensional (numeric)
>> arrays are not automatically treated as (numeric) vectors
>> i.e. class "numeric" wrt S4 methods.
>> 
>> In the following case the solve() - coincidence does not
>> help, BTW.
>> 
>> Diagonal(3) %*% array(1:3)
>> 
>> ## Error in Diagonal(3) %*% array(1:3) : ##
>> not-yet-implemented method for  %*% 
>> 
>> 
>> In principle, we should consider a way to tell that
>> "array" should be tried as "vector",

> Actually, if you think compatible 1-d numeric arrays
> should 'work', then all I was thinking of was something
> along the following lines: Add an additional

> setMethod("solve", c("ANY", "array"), function(a, b, ...)
> ...

> which would basically do a dimension check for b, for 1-d
> numeric arrays call solve(a, as.vector(b), ...), and error
> out for dim(b) > 2.  The actual details may be more
> involved, but that's the basic idea.

Well, of course, it's just that a method like the one above
would have to be provided for all signatures where something
corresponding for "numeric" now exists ..
and these may easily get to many dozens at least.

In other words  solve() is just one special case of very many
and I was rather interested in finding a method to solve "all
cases" at once - "automagically".

Otherwise, supporting  1d "array" to work Matrix-pkg matrices
would not only mean providing these many dozen of methods now,
but
also for every new functionality that uses S4 methods with
numeric vectors to have to always provide an additional "array"
method, i.e.,  increasing the Matrix-maintenance burden yet more.




>> possibly via something like setIs("array", "vector") or
>> rather setIs("array", "numeric") because in the Matrix
>> package the vectors encountered are really numeric
>> vectors.
>> 
>> .. OTOH, in all of the 3 packages I co-author and which
>> use S4 heavily, Matrix, Rmpfr, lme4, I had till now
>> decided *not* to setIs() because it never worked as I
>> intended, or rather had unpleasant side effects.
>> 
>> Here, setIs("array", "numeric", test=is.numeric)
>> 
>> gives
>> 
>> Error in setIs("array", "numeric", test = is.numeric) :
>> cannot create a 'setIs' relation when neither of the
>> classes (“array” and “numeric”) is local and modifiable
>> in this package
>> 
>> A more successful alternative had been to use
>> setClassUnion(), so I could consider defining
>> 
>> setClassUnion("mVector", c("numeric", "array"))
>> 
>> and replace "numeric" in many of the method signatures by
>> "mVector" (but that would then also dispatch for 1d
>> character arrays ... not so nicely).

> But you already have that problem, I think:

>> s = matrix(letters[1:10], 5, 2) solve(Diagonal(5), s)
> Error in .M.kind(data) : not yet implemented for matrix
> with typeof character

> whereas

> m = matrix(1:10, 5, 2)

> works nicely. Unfortunately, both m and s have the same
> class (c("matrix", "array")), so I don't think method
> dispatch would be able to distinguish between them with
> the current design, and you anyway need to check in the
> solve method for c("diagonalMatrix", "matrix").

> Best, 

Re: [R] Matrix::solve() with 1-d arrays -- treating "array" as "numeric"?

2021-04-18 Thread Deepayan Sarkar
On Sat, Apr 17, 2021 at 9:08 PM Martin Maechler
 wrote:
>
> > Deepayan Sarkar
> > on Fri, 16 Apr 2021 11:34:20 +0530 writes:
>
> > I get what I initially thought was unexpected behaviour from:
>
> > x <- tapply(runif(100), sample(5, 100, TRUE), mean)
> > solve(Diagonal(5), x)
> > # Error: not-yet-implemented method for solve(, ).
> > #  ->>  Ask the package authors to implement the missing feature.
>
> ((why did you not ask the package authors ?? --- never mind))
>
>
> > This is because x is a 1-D array, so the operation is not
> > well-defined. Would it make sense for Matrix to support this (treat
> > 1-D arrays as column vectors, as it does for plain vectors)? Or should
> > I make my intent clear with
>
> > solve(Diagonal(5), as.vector(x))
>
> well ...
>
> The "fun" thing is that it actually works when Matrix methods
> are not fully available, i.e., if you do *not* do
> require(Matrix) or equivalent,
> but rather only load the Matrix namespace via
>
>   solve(Matrix::Diagonal(5), x)
>
> actually currently works correctly by some "good coincidence"
> (I have not yet tried to understand, as that's a bit painful:
>  selectMethod("solve", c("ddiMatrix", "array"))  is "lying" here ! )
>
> However this looks like a more general problem with S4 methods
> -- and probably a good reason for asking on R-help --  namely,
> the fact that d1-dimensional (numeric) arrays are not automatically treated as
> (numeric) vectors i.e. class "numeric"  wrt S4 methods.
>
> In the following case the  solve() - coincidence does not help, BTW.
>
>  Diagonal(3) %*% array(1:3)
>
>  ## Error in Diagonal(3) %*% array(1:3) :
>  ##   not-yet-implemented method for  %*% 
>
>
> In principle, we should consider a way to tell that "array"
> should be tried as "vector",

Actually, if you think compatible 1-d numeric arrays should 'work',
then all I was thinking of was something along the following lines:
Add an additional

setMethod("solve", c("ANY", "array"), function(a, b, ...) ...

which would basically do a dimension check for b, for 1-d numeric
arrays call solve(a, as.vector(b), ...), and error out for dim(b) > 2.
The actual details may be more involved, but that's the basic idea.

> possibly via something likesetIs("array", "vector")  or
> rather  setIs("array", "numeric")  because in the Matrix package
> the vectors encountered are really numeric vectors.
>
> .. OTOH, in all of the 3 packages I co-author and which use S4  heavily,  
> Matrix, Rmpfr, lme4,
> I had till now decided *not* to  setIs()  because it never
> worked as I intended, or rather had unpleasant side effects.
>
> Here,
>   setIs("array", "numeric", test=is.numeric)
>
> gives
>
> Error in setIs("array", "numeric", test = is.numeric) :
> cannot create a 'setIs' relation when neither of the classes (“array” and 
> “numeric”) is local and modifiable in this package
>
> A more successful alternative had been to use  setClassUnion(),
> so I could consider defining
>
>   setClassUnion("mVector", c("numeric", "array"))
>
> and replace "numeric" in many of the method signatures by  "mVector"
> (but that would then also dispatch for 1d character arrays
>  ... not so nicely).

But you already have that problem, I think:

> s = matrix(letters[1:10], 5, 2)
> solve(Diagonal(5), s)
Error in .M.kind(data) :
  not yet implemented for matrix with typeof character

whereas

m = matrix(1:10, 5, 2)

works nicely. Unfortunately, both m and s have the same class
(c("matrix", "array")), so I don't think method dispatch would be able
to distinguish between them with the current design, and you anyway
need to check in the solve method for c("diagonalMatrix", "matrix").

Best,
-Deepayan


> > -Deepayan
>
> > __
> > 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] Matrix::solve() with 1-d arrays -- treating "array" as "numeric"?

2021-04-17 Thread Martin Maechler
> Deepayan Sarkar 
> on Fri, 16 Apr 2021 11:34:20 +0530 writes:

> I get what I initially thought was unexpected behaviour from:

> x <- tapply(runif(100), sample(5, 100, TRUE), mean)
> solve(Diagonal(5), x)
> # Error: not-yet-implemented method for solve(, ).
> #  ->>  Ask the package authors to implement the missing feature.

((why did you not ask the package authors ?? --- never mind))


> This is because x is a 1-D array, so the operation is not
> well-defined. Would it make sense for Matrix to support this (treat
> 1-D arrays as column vectors, as it does for plain vectors)? Or should
> I make my intent clear with

> solve(Diagonal(5), as.vector(x))

well ...

The "fun" thing is that it actually works when Matrix methods
are not fully available, i.e., if you do *not* do
require(Matrix) or equivalent,
but rather only load the Matrix namespace via

  solve(Matrix::Diagonal(5), x)

actually currently works correctly by some "good coincidence"
(I have not yet tried to understand, as that's a bit painful:
 selectMethod("solve", c("ddiMatrix", "array"))  is "lying" here ! )
   
However this looks like a more general problem with S4 methods
-- and probably a good reason for asking on R-help --  namely,
the fact that d1-dimensional (numeric) arrays are not automatically treated as
(numeric) vectors i.e. class "numeric"  wrt S4 methods.

In the following case the  solve() - coincidence does not help, BTW.

 Diagonal(3) %*% array(1:3)

 ## Error in Diagonal(3) %*% array(1:3) : 
 ##   not-yet-implemented method for  %*% 


In principle, we should consider a way to tell that "array"
should be tried as "vector",

possibly via something likesetIs("array", "vector")  or
rather  setIs("array", "numeric")  because in the Matrix package
the vectors encountered are really numeric vectors.

.. OTOH, in all of the 3 packages I co-author and which use S4  heavily,  
Matrix, Rmpfr, lme4,
I had till now decided *not* to  setIs()  because it never
worked as I intended, or rather had unpleasant side effects.

Here,
  setIs("array", "numeric", test=is.numeric)

gives

Error in setIs("array", "numeric", test = is.numeric) : 
cannot create a 'setIs' relation when neither of the classes (“array” and 
“numeric”) is local and modifiable in this package

A more successful alternative had been to use  setClassUnion(),
so I could consider defining

  setClassUnion("mVector", c("numeric", "array"))

and replace "numeric" in many of the method signatures by  "mVector"
(but that would then also dispatch for 1d character arrays
 ... not so nicely).



> -Deepayan

> __
> 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] Matrix - remove [,1] from top row

2019-07-02 Thread Richard O'Keefe
(1) m[,1] is the first column of matrix (or dataframe) m.
(2) The first row of matrix or dataframe m is m[1,]
(3) To remove the first row of matrix or dataframe m,
do m <- m[-1,]


On Wed, 3 Jul 2019 at 08:59, Nicola Cecchino  wrote:

> Hello,
>
> I am simply trying to remove the [,1] row from a matrix.  I tried to
> search Google to see if I could find how that is removed, but could
> not find a way to do it.
>
> So I have the following:
>
> [,1]
> date  2019-7-01
> Peeps   5
> days 7
> worn  9
>
> this is what I want:
>
> date  2019-7-01
> Peeps   5
> days 7
> worn  9
>
> Any ideas?
>
> Nic
>
> __
> 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] Matrix - remove [,1] from top row

2019-07-02 Thread Richard M. Heiberger
give your a matrix an empty column name.

> tmp <- matrix(1:4, 4, 1, dimnames=list(letters[1:4], NULL))
> tmp
  [,1]
a1
b2
c3
d4
> dimnames(tmp)[[1]]
[1] "a" "b" "c" "d"
> dimnames(tmp)[[2]]
NULL
> dimnames(tmp)[[2]] <- ""
> tmp

a 1
b 2
c 3
d 4

On Tue, Jul 2, 2019 at 5:09 PM  wrote:
>
> Hello,
>
> That is not a row, what you seem to have is an object of class
> "matrix" and when it's printed it prints the column names or [,1]
> [,2] etc if there aren't any colnames. So your matrix has just one
> column and 4 rows with rownames 'date', 'Peeps', 'days', 'worn'.
>
>
> Hope this helps,
>
> Rui Barradas
>
>
>
>
> Citando Nicola Cecchino :
>
> > Hello,
> >
> > I am simply trying to remove the [,1] row from a matrix.  I tried to
> > search Google to see if I could find how that is removed, but could
> > not find a way to do it.
> >
> > So I have the following:
> >
> > [,1]
> > date  2019-7-01
> > Peeps   5
> > days 7
> > worn  9
> >
> > this is what I want:
> >
> > date  2019-7-01
> > Peeps   5
> > days 7
> > worn  9
> >
> > Any ideas?
> >
> > Nic
> >
> > __
> > 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.

__
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] Matrix - remove [,1] from top row

2019-07-02 Thread ruipbarradas

Hello,

That is not a row, what you seem to have is an object of class  
"matrix" and when it's printed it prints the column names or [,1]   
[,2] etc if there aren't any colnames. So your matrix has just one  
column and 4 rows with rownames 'date', 'Peeps', 'days', 'worn'.



Hope this helps,

Rui Barradas




Citando Nicola Cecchino :


Hello,

I am simply trying to remove the [,1] row from a matrix.  I tried to
search Google to see if I could find how that is removed, but could
not find a way to do it.

So I have the following:

[,1]
date  2019-7-01
Peeps   5
days 7
worn  9

this is what I want:

date  2019-7-01
Peeps   5
days 7
worn  9

Any ideas?

Nic

__
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] Matrix::bdiag doesn't like being given a single named argument

2019-05-31 Thread Jeff Newmiller
My take would be that there is no reason to specify argument names at all when 
calling bdiag, and clearly there is a reason to not do so.

The error seems to arise from using the S3 method is.list, which allows your 
final example to work. is.list(a=1) clearly fails to match the x argument, but 
is.list(list(a=1)) works just fine.

On May 31, 2019 6:25:19 AM PDT, Benjamin Tyner  wrote:
>Hello,
>
>Perhaps not a bug, but interesting because the error only happens when 
>there is a single named argument.
>
>    > m <- matrix(1, 1, 1)
>    > library(Matrix)
>    > bdiag(m)
>    1 x 1 sparse Matrix of class "dgCMatrix"
>
>    [1,] 1
>    > bdiag(a = m)
>   Error in is.list(...) : supplied argument name 'a' does not match
>'x'
>    > bdiag(a = m, b = m)
>    2 x 2 sparse Matrix of class "dgCMatrix"
>
>    [1,] 1 .
>    [2,] . 1
>
>Moreover, this works fine:
>
>    > bdiag(list(a = m))
>    1 x 1 sparse Matrix of class "dgCMatrix"
>
>    [1,] 1
>
>Thoughts?
>
>Regards
>
>Ben
>
>__
>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] matrix subset problem with factors

2019-02-20 Thread Jeff Newmiller
With on official weight, I second the opinion that the existing behavior is 
appropriate and not a bug.

Functions should not "unexpectedly" return factors... a common example are the 
read.table family of functions that by default return factors, but the 
behaviour is deterministic and controllable with the as.is or stringsAsFactors 
arguments. If you have functions that randomly return different types then the 
bug is in those functions.

Don't confuse factors and character data types... they are distinct and used 
for different purposes.

On February 20, 2019 12:59:54 PM PST, Marc Schwartz via R-help 
 wrote:
>Hi,
>
>I get the same behavior in R 3.5.2 on macOS.
>
>Others may feel differently, but I am not so sure that this is a bug,
>as opposed to perhaps the need to clarify in ?Extract, that the
>following, which is found under Atomic vectors:
>
>"The index object i can be numeric, logical, character or empty.
>Indexing by factors is allowed and is equivalent to indexing by the
>numeric codes (see factor) and not by the character values which are
>printed (for which use [as.character(i)])."
>
>also applies to the indexing of matrices and arrays.
>
>Since matrices and arrays in R are vectors with 'dim' attributes, the
>behavior is essentially consistent as described above.
>
>Thus, perhaps just add the second sentence above or similar wording to
>the section for Matrices and arrays.
>
>Regards,
>
>Marc Schwartz
>
>> On Feb 20, 2019, at 4:23 AM, ঋষি ( ऋषि / rIsHi )
> wrote:
>> 
>> Hi All,
>> 
>> I like to report this bug related to matrix subset by rownames when
>passed
>> as factors. Now factors are may not be safe to use but then it should
>> generate a warning message. Since many time we use values returned by
>some
>> packages as factor to subset a matrix and which may result in a wrong
>> calculation.
>> 
>> I wish if "factor" is not expected in matrix operation then it should
>throw
>> an error/warning message.
>> 
>> Below are the codes to reproduce it.
>> 
>>> x <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"),
>> c("A","B","C")))
>>> 
>>> rNames <- as.factor(c("X","Z"))
>>> # As some functions from different packages return factors and which
>> could be overlooked
>>> rNames
>> [1] X Z
>> Levels: X Z
>>> 
>>> x[rNames,]
>>  A B C
>> X 1 4 7
>> Y 2 5 8
>>> 
>>> ## The intended matrix should return X and Z rows instead of X and Y
>>> 
>>> sessionInfo()
>> R version 3.4.1 (2017-06-30)
>> Platform: x86_64-pc-linux-gnu (64-bit)
>> Running under: Ubuntu 14.04.5 LTS
>> 
>> Matrix products: default
>> BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
>> LAPACK: /usr/lib/lapack/liblapack.so.3.0
>> 
>> locale:
>> [1] LC_CTYPE=en_GB.UTF-8   LC_NUMERIC=C
>> [3] LC_TIME=en_GB.UTF-8LC_COLLATE=en_GB.UTF-8
>> [5] LC_MONETARY=en_GB.UTF-8LC_MESSAGES=en_GB.UTF-8
>> [7] LC_PAPER=en_GB.UTF-8   LC_NAME=C
>> [9] LC_ADDRESS=C   LC_TELEPHONE=C
>> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
>> 
>> attached base packages:
>> [1] stats graphics  grDevices utils datasets  methods   base
>> 
>> loaded via a namespace (and not attached):
>> [1] compiler_3.4.1
>>> 
>> 
>> 
>> 
>> With regards
>> Rishi Das Roy
>
>__
>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] matrix subset problem with factors

2019-02-20 Thread Marc Schwartz via R-help
Hi,

I get the same behavior in R 3.5.2 on macOS.

Others may feel differently, but I am not so sure that this is a bug, as 
opposed to perhaps the need to clarify in ?Extract, that the following, which 
is found under Atomic vectors:

"The index object i can be numeric, logical, character or empty. Indexing by 
factors is allowed and is equivalent to indexing by the numeric codes (see 
factor) and not by the character values which are printed (for which use 
[as.character(i)])."

also applies to the indexing of matrices and arrays.

Since matrices and arrays in R are vectors with 'dim' attributes, the behavior 
is essentially consistent as described above.

Thus, perhaps just add the second sentence above or similar wording to the 
section for Matrices and arrays.

Regards,

Marc Schwartz

> On Feb 20, 2019, at 4:23 AM, ঋষি ( ऋषि / rIsHi )  
> wrote:
> 
> Hi All,
> 
> I like to report this bug related to matrix subset by rownames when passed
> as factors. Now factors are may not be safe to use but then it should
> generate a warning message. Since many time we use values returned by some
> packages as factor to subset a matrix and which may result in a wrong
> calculation.
> 
> I wish if "factor" is not expected in matrix operation then it should throw
> an error/warning message.
> 
> Below are the codes to reproduce it.
> 
>> x <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"),
> c("A","B","C")))
>> 
>> rNames <- as.factor(c("X","Z"))
>> # As some functions from different packages return factors and which
> could be overlooked
>> rNames
> [1] X Z
> Levels: X Z
>> 
>> x[rNames,]
>  A B C
> X 1 4 7
> Y 2 5 8
>> 
>> ## The intended matrix should return X and Z rows instead of X and Y
>> 
>> sessionInfo()
> R version 3.4.1 (2017-06-30)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 14.04.5 LTS
> 
> Matrix products: default
> BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
> LAPACK: /usr/lib/lapack/liblapack.so.3.0
> 
> locale:
> [1] LC_CTYPE=en_GB.UTF-8   LC_NUMERIC=C
> [3] LC_TIME=en_GB.UTF-8LC_COLLATE=en_GB.UTF-8
> [5] LC_MONETARY=en_GB.UTF-8LC_MESSAGES=en_GB.UTF-8
> [7] LC_PAPER=en_GB.UTF-8   LC_NAME=C
> [9] LC_ADDRESS=C   LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
> 
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
> 
> loaded via a namespace (and not attached):
> [1] compiler_3.4.1
>> 
> 
> 
> 
> With regards
> Rishi Das Roy

__
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] Matrix logical operator

2017-07-16 Thread Berend Hasselman

> On 17 Jul 2017, at 07:27, Jeremie Juste  wrote:
> 
> 
> Hello,
> 
> I have some trouble understanding why !b & is TRUE. Do you have an idea?
> 
> 
>> b <- matrix(c(0,1,1,0,1,0),2)
> 
>> !b
>  [,1]  [,2]  [,3]
> [1,]  TRUE FALSE FALSE
> [2,] FALSE  TRUE  TRUE
>> !b &
> [1] TRUE
> 

Read the help for &&. You can see it like this: ?`&&`
Try

!b[1] && TRUE

and

!b[2] && TRUE


Berend hasselman

> Best regards,
> 
> Jeremie
> 
> __
> 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] Matrix multiplication

2017-06-07 Thread Jeff Newmiller
Fine,  except that you already seen to have a very compact solution if that 
really is what you are looking for. What am I missing? 
-- 
Sent from my phone. Please excuse my brevity.

On June 7, 2017 9:16:48 PM PDT, Steven Yen  wrote:
>OK Thanks. Your response made me think. Here (the last line) is what I
>need:
>
>set.seed(76543211)
>w<-1:10; w
>a<-matrix(rpois(20,2),nrow=10); a
>t(w*a)%*%a
>
>On 6/8/2017 12:09 PM, Jeff Newmiller wrote:
>> Is this a question? You seem to have three possible calculations,
>have already implemented two of them (?) and it is unclear (to me) what
>you think the right answer for any of them is supposed to be.
>> -- Sent from my phone. Please excuse my brevity. On June 7, 2017 
>> 8:50:55 PM PDT, Steven Yen  wrote:
>>> I need to have all elements of a matrix multiplied by a weight
>before
>>> being post-multiplied by itself, as shown in the forst block of
>codes
>>> below. I can also multiply the matrix by the square root of the
>weight
>>> and then take the outer product.
>>>
>>> Actually, what I need is this. Denote each row of the matrix by a
>row
>>> vector as xi and each element of the weighting vector as wi. Then, I
>>> need the sum of wi * t(xi) %*% xi over i.
>>>
>>> Any suggestion for a compact approach would be appreciated.
>>>
>>> set.seed(76543211)
>>> w<-1:10; w
>>> a<-matrix(rpois(20,2),nrow=10); a
>>> b<-a
>>> a<-w*a
>>> t(a)%*%b
>>>
>>> set.seed(76543211)
>>> a<-matrix(rpois(20,2),nrow=10); a
>>> a<-sqrt(w)*a; a
>>> t(a)%*%a

__
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] Matrix multiplication

2017-06-07 Thread Steven Yen
OK Thanks. Your response made me think. Here (the last line) is what I need:

set.seed(76543211)
w<-1:10; w
a<-matrix(rpois(20,2),nrow=10); a
t(w*a)%*%a

On 6/8/2017 12:09 PM, Jeff Newmiller wrote:
> Is this a question? You seem to have three possible calculations, have 
> already implemented two of them (?) and it is unclear (to me) what you think 
> the right answer for any of them is supposed to be.
> -- Sent from my phone. Please excuse my brevity. On June 7, 2017 
> 8:50:55 PM PDT, Steven Yen  wrote:
>> I need to have all elements of a matrix multiplied by a weight before
>> being post-multiplied by itself, as shown in the forst block of codes
>> below. I can also multiply the matrix by the square root of the weight
>> and then take the outer product.
>>
>> Actually, what I need is this. Denote each row of the matrix by a row
>> vector as xi and each element of the weighting vector as wi. Then, I
>> need the sum of wi * t(xi) %*% xi over i.
>>
>> Any suggestion for a compact approach would be appreciated.
>>
>> set.seed(76543211)
>> w<-1:10; w
>> a<-matrix(rpois(20,2),nrow=10); a
>> b<-a
>> a<-w*a
>> t(a)%*%b
>>
>> set.seed(76543211)
>> a<-matrix(rpois(20,2),nrow=10); a
>> a<-sqrt(w)*a; a
>> t(a)%*%a


[[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] Matrix multiplication

2017-06-07 Thread Jeff Newmiller
Is this a question? You seem to have three possible calculations, have already 
implemented two of them (?) and it is unclear (to me) what you think the right 
answer for any of them is supposed to be. 
-- 
Sent from my phone. Please excuse my brevity.

On June 7, 2017 8:50:55 PM PDT, Steven Yen  wrote:
>I need to have all elements of a matrix multiplied by a weight before 
>being post-multiplied by itself, as shown in the forst block of codes 
>below. I can also multiply the matrix by the square root of the weight 
>and then take the outer product.
>
>Actually, what I need is this. Denote each row of the matrix by a row 
>vector as xi and each element of the weighting vector as wi. Then, I 
>need the sum of wi * t(xi) %*% xi over i.
>
>Any suggestion for a compact approach would be appreciated.
>
>set.seed(76543211)
>w<-1:10; w
>a<-matrix(rpois(20,2),nrow=10); a
>b<-a
>a<-w*a
>t(a)%*%b
>
>set.seed(76543211)
>a<-matrix(rpois(20,2),nrow=10); a
>a<-sqrt(w)*a; a
>t(a)%*%a
>
>
>
>
>On 1/4/2017 5:41 PM, Steven Yen wrote:
>> I need help with gls{nlme}.
>> Specifically, I am estimating an equation with AR(1) using 
>> maximum-likelihood. I am not understanding the correlationoption 
>> below. Help appreciated.
>>
>> ===
>> library(nlme)
>> eq1<-log(chnimp)~log(chempi)+log(gas)+log(rtwex)+befile6+
>>  affile6+afdec6
>> reg1<-gls(eq1,data=mydata,correlation=corAR1(),method="ML",verbose=T)
>>
>
>   [[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] Matrix-list table conversion+nrwos with specefic values.

2017-04-29 Thread Jeff Newmiller
Please use reply-all to make sure the mailing list is included so others can 
learn from the discussion. 

Yes, you can use row and column names. Have you tried it? 
-- 
Sent from my phone. Please excuse my brevity.

On April 29, 2017 2:44:06 PM PDT, abo dalash <abo_d...@hotmail.com> wrote:
>Many thanks Jeff for your rapid response.
>
>
>Reg. the first task, I will discuss this later with you as I have faced
>some issues.
>
>
>Your guidance reg. Matrix-Table conversion has produced the table I
>want.
>
>thank you so much. My matrix is of size 120*120 and the headers of the
>columns and rows contain the same list of drug names. The table I
>produced
>
>contains the number of rows and number of columns in the matrix under
>
>r and c columns in my produced table. Is it possible to have names of
>drugs
>
>in my table instead of the number of row or number of column of the
>matrix.
>
>Please let me know if this is not clear.
>
>
>Many thanks
>
>
>
>From: Jeff Newmiller <jdnew...@dcn.davis.ca.us>
>Sent: 29 April 2017 10:11 PM
>To: r-help@r-project.org; Bert Gunter; abo dalash; R-help
>Subject: Re: [R] Matrix-list table conversion+nrwos with specefic
>values.
>
>Break it down. If you have a scalar value val and you want to know if
>it is in a vector vec, using val==vec gets you a logical vector as long
>as vec. You can use val %in% vec and you get a logical vector as long
>as val (e.g. 1). If val is a vector of, say, length 2, then you will
>get a length 2 logical vector. In your example, c(566,23) %in% c(123,
>566, 235) would be c( TRUE, TRUE ). Since you want both of these
>elements to be TRUE,  you can use the all() function as in all(
>c(566,23) %in% c(123, 566, 235) ). So use the apply function to examine
>each row as a vector and you have it:
>
>apply( mat, 1, function(v) { all( c( 566,235) %in% v ) } )
>
>Note that this only works if your data are integers (see FAQ 7.31).
>
>Re the matrix to table... use expand.grid and matrix indexing.
>
>tbl <- expand.grid( r = seq.int( nrow( mat ) )
>  , c = seq.int( ncol( mat ) ) )
>tbl$val <- mat[ as.matrix( tbl[ , c( "r","c" ) ] ) ]
>tbl[ order( tbl$val, decreasing=TRUE), ]
>
>--
>Sent from my phone. Please excuse my brevity.
>
>On April 29, 2017 9:53:08 AM PDT, Bert Gunter <bgunter.4...@gmail.com>
>wrote:
>>I am not a private (or free!) consultant. Post to the r-help if your
>>question concerns R.
>>
>>-- Bert
>>
>>Bert Gunter
>>
>>
>>
>>On Sat, Apr 29, 2017 at 8:51 AM, abo dalash <abo_d...@hotmail.com>
>>wrote:
>>> Hi dear Bert
>>>
>>>
>>> I'm trying to identify number of rows containing 2 specific values.
>>>
>>> I tried : which(mydata == 566,235), but this returns logical values
>>for all
>>> rows and any T in a certain row indicates the existence of one of
>>these
>>> values but what I need to know is only number of rows in my data set
>>with
>>> these 2 particular values considering these two values
>>>
>>> as one pair per row. For example :
>>>
>>>
>>> 1  123   566235
>>>
>>> 2  44354  566
>>>
>>> 3  56644  235
>>>
>>>
>>> here number of rows with the values 566&235 is 2 which are
>>>
>>> rows 1 & 3. Row 2 has only 566 so it should not be included in
>>>
>>> our calculation.
>>>
>>>
>>> I also have a large matrix and wanted to convert it into a table so
>I
>>can
>>>
>>> easily identify the combination with higher frequencies.
>>>
>>>
>>> The matrix looks like this:
>>>
>>>
>>> x  y  z
>>>
>>> x  0  5   67
>>>
>>> y  na0  23
>>>
>>> z   na   na  0
>>>
>>>
>>> and I would like to convert this into a table arranged with
>>>
>>> higher values first like this :
>>>
>>> x   z   67
>>>
>>> y   z   23
>>>
>>> x   y5
>>>
>>> x   x0
>>>
>>> y   y0
>>>
>>> zz0
>>>
>>> yxna
>>>
>>> zxna
>>>
>>> zyna
>>>
>>>
>>> Is there a simple function to perf

Re: [R] Matrix-list table conversion+nrwos with specefic values.

2017-04-29 Thread Jeff Newmiller
Break it down. If you have a scalar value val and you want to know if it is in 
a vector vec, using val==vec gets you a logical vector as long as vec. You can 
use val %in% vec and you get a logical vector as long as val (e.g. 1). If val 
is a vector of, say, length 2, then you will get a length 2 logical vector. In 
your example, c(566,23) %in% c(123, 566, 235) would be c( TRUE, TRUE ). Since 
you want both of these elements to be TRUE,  you can use the all() function as 
in all( c(566,23) %in% c(123, 566, 235) ). So use the apply function to examine 
each row as a vector and you have it:

apply( mat, 1, function(v) { all( c( 566,235) %in% v ) } )

Note that this only works if your data are integers (see FAQ 7.31).

Re the matrix to table... use expand.grid and matrix indexing.

tbl <- expand.grid( r = seq.int( nrow( mat ) )
  , c = seq.int( ncol( mat ) ) )
tbl$val <- mat[ as.matrix( tbl[ , c( "r","c" ) ] ) ]
tbl[ order( tbl$val, decreasing=TRUE), ]

-- 
Sent from my phone. Please excuse my brevity.

On April 29, 2017 9:53:08 AM PDT, Bert Gunter  wrote:
>I am not a private (or free!) consultant. Post to the r-help if your
>question concerns R.
>
>-- Bert
>
>Bert Gunter
>
>
>
>On Sat, Apr 29, 2017 at 8:51 AM, abo dalash 
>wrote:
>> Hi dear Bert
>>
>>
>> I'm trying to identify number of rows containing 2 specific values.
>>
>> I tried : which(mydata == 566,235), but this returns logical values
>for all
>> rows and any T in a certain row indicates the existence of one of
>these
>> values but what I need to know is only number of rows in my data set
>with
>> these 2 particular values considering these two values
>>
>> as one pair per row. For example :
>>
>>
>> 1  123   566235
>>
>> 2  44354  566
>>
>> 3  56644  235
>>
>>
>> here number of rows with the values 566&235 is 2 which are
>>
>> rows 1 & 3. Row 2 has only 566 so it should not be included in
>>
>> our calculation.
>>
>>
>> I also have a large matrix and wanted to convert it into a table so I
>can
>>
>> easily identify the combination with higher frequencies.
>>
>>
>> The matrix looks like this:
>>
>>
>> x  y  z
>>
>> x  0  5   67
>>
>> y  na0  23
>>
>> z   na   na  0
>>
>>
>> and I would like to convert this into a table arranged with
>>
>> higher values first like this :
>>
>> x   z   67
>>
>> y   z   23
>>
>> x   y5
>>
>> x   x0
>>
>> y   y0
>>
>> zz0
>>
>> yxna
>>
>> zxna
>>
>> zyna
>>
>>
>> Is there a simple function to perform this conversion with some
>explanation
>> about the Syntax if you don't mind?
>>
>>
>> Regards
>
>__
>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] Matrix-list table conversion+nrwos with specefic values.

2017-04-29 Thread Bert Gunter
I am not a private (or free!) consultant. Post to the r-help if your
question concerns R.

-- Bert

Bert Gunter



On Sat, Apr 29, 2017 at 8:51 AM, abo dalash  wrote:
> Hi dear Bert
>
>
> I'm trying to identify number of rows containing 2 specific values.
>
> I tried : which(mydata == 566,235), but this returns logical values for all
> rows and any T in a certain row indicates the existence of one of these
> values but what I need to know is only number of rows in my data set with
> these 2 particular values considering these two values
>
> as one pair per row. For example :
>
>
> 1  123   566235
>
> 2  44354  566
>
> 3  56644  235
>
>
> here number of rows with the values 566&235 is 2 which are
>
> rows 1 & 3. Row 2 has only 566 so it should not be included in
>
> our calculation.
>
>
> I also have a large matrix and wanted to convert it into a table so I can
>
> easily identify the combination with higher frequencies.
>
>
> The matrix looks like this:
>
>
> x  y  z
>
> x  0  5   67
>
> y  na0  23
>
> z   na   na  0
>
>
> and I would like to convert this into a table arranged with
>
> higher values first like this :
>
> x   z   67
>
> y   z   23
>
> x   y5
>
> x   x0
>
> y   y0
>
> zz0
>
> yxna
>
> zxna
>
> zyna
>
>
> Is there a simple function to perform this conversion with some explanation
> about the Syntax if you don't mind?
>
>
> Regards

__
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] matrix merge, or something else?

2017-03-10 Thread Evan Cooch

Slick -- thanks.

On 3/10/2017 1:26 AM, Jeff Newmiller wrote:

test2[ , colnames( test1 ) ] <- test1


__
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] matrix merge, or something else?

2017-03-09 Thread Jeff Newmiller
test2[ , colnames( test1 ) ] <- test1
-- 
Sent from my phone. Please excuse my brevity.

On March 9, 2017 6:56:13 PM PST, Evan Cooch  wrote:
>Suppose I have the following two matrices, both with same number of
>rows 
>(3), but different number of columns (3 in test1, 4 in test2).
>
>test1 <- matrix(c(1,1,0,1,0,-1,-1,-1,0),3,3,byrow=T);
>test2 <- matrix( rep( 0, len=12), nrow = 3)
>
>I label the rows and columns of the two matrices as follows:
>
>rownames(test1) <- c("row1","row2","row3")
>rownames(test2) <- c("row1","row2","row3")
>
>colnames(test1) <- c("a","b","d")
>colnames(test2) <- c("a","b","c","d")
>
>So, if we look at the matrices, we see
>
>test1
>
>  a  b  d
>row1   1  1  0
>row2   1  0 -1
>row3  -1 -1  0
>
>
>test2
>
> a b c d
>row1  0 0 0 0
>row2  0 0 0 0
>row3  0 0 0 0
>
>So, we see that while both matrices have the same rows, the matrix
>test1 
>has a subset of the columns of test2. In test1, there is no column for 
>'c' -- have columns for 'a', 'b', 'd'.
>
>Now, what I want to do is this -- take the information from each column
>
>in test1, and substitute it into the same row/column in test2. The end 
>result should be a matrix that looks like:
>
> a  b  c  d
>row1  1  1  0  0
>row2  1  0  0 -1
>row3 -1 -1 0   0
>
>My initial though  was some sort of merge by row and column, with some 
>funky sort of intersection, but I couldn't figure out how to get that
>to 
>work.
>
>Any suggestions/pointers to the obvious most appreciated.
>
>__
>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] Matrix

2017-03-07 Thread David L Carlson
Change the "4" in embed(c(0*x[-1], x, 0*x[-1]), 4) to length(x) and it will 
generalize to other length vectors. This solution is not as compact, but it 
illustrates a relatively obscure R function:

> x <- 1:4
> ncol <- length(x)
> zeros <- rep(0, ncol - 1)
> toeplitz(c(zeros, x, zeros))[-(1:(ncol-1)), 1:ncol]
 [,1] [,2] [,3] [,4]
[1,]1000
[2,]2100
[3,]3210
[4,]4321
[5,]0432
[6,]0043
[7,]0004

-
David L Carlson
Department of Anthropology
Texas A University
College Station, TX 77840-4352

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Gabor 
Grothendieck
Sent: Tuesday, March 7, 2017 7:21 AM
To: Peter Thuresson <peter.thures...@umea.se>
Cc: R-help@r-project.org
Subject: Re: [R] Matrix

Assuming that the input is x <- 1:4, try this one-liner:

> embed(c(0*x[-1], x, 0*x[-1]), 4)
 [,1] [,2] [,3] [,4]
[1,]1000
[2,]2100
[3,]3210
[4,]4321
[5,]0432
[6,]0043
[7,]0004

On Mon, Mar 6, 2017 at 11:18 AM, Peter Thuresson
<peter.thures...@umea.se> wrote:
> Hello,
>
> Is there a function in R which can transform, let say a vector:
>
> c(1:4)
>
> to a matrix where the vector is repeated but "projected" +1 one step down for 
> every (new) column.
> I want the output below from the vector above, like this:
>
> p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)
>
> matrix(p,7,4)
>

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
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] Matrix

2017-03-07 Thread Gabor Grothendieck
Assuming that the input is x <- 1:4, try this one-liner:

> embed(c(0*x[-1], x, 0*x[-1]), 4)
 [,1] [,2] [,3] [,4]
[1,]1000
[2,]2100
[3,]3210
[4,]4321
[5,]0432
[6,]0043
[7,]0004

On Mon, Mar 6, 2017 at 11:18 AM, Peter Thuresson
 wrote:
> Hello,
>
> Is there a function in R which can transform, let say a vector:
>
> c(1:4)
>
> to a matrix where the vector is repeated but "projected" +1 one step down for 
> every (new) column.
> I want the output below from the vector above, like this:
>
> p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)
>
> matrix(p,7,4)
>

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
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] Matrix

2017-03-06 Thread Richard M. Heiberger
## 1.
## This could be captured into a function

tmp <- matrix(0, 7, 4)
tmp
diag(tmp) <- 1
diag(tmp[-1,]) <- 2
diag(tmp[-(1:2),]) <- 3
diag(tmp[-(1:3),]) <- 4
tmp


## 2.
v <- 1:4
v2 <- c(v, rep(0, length(v)))
## this generates a warning that can safely be ignored (or turned off)
matrix(v2, length(v2)-1, length(v))

On Mon, Mar 6, 2017 at 11:18 AM, Peter Thuresson
 wrote:
> Hello,
>
> Is there a function in R which can transform, let say a vector:
>
> c(1:4)
>
> to a matrix where the vector is repeated but "projected" +1 one step down for 
> every (new) column.
> I want the output below from the vector above, like this:
>
> p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)
>
> matrix(p,7,4)
>
> best regards / Peter
>
> [[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] Matrix

2017-03-06 Thread Mathew Guilfoyle
Effectively you want a circulant matrix but filled by column.

Example input vector and number of columns

x = c(1:8,19:20)
nc = 5

For the result you specifically describe, the following generalises for any 
vector and any arbitrary number of columns 'nc', padding with zeros as 
necessary.

matrix(c(rep(c(x,rep(0,nc)),nc-1),x), ncol=nc)


For the unpadded column circulant on the same inputs

nx = length(x)
ind = 1+ outer(0:(nx-1),0:(1-nc),'+') %% nx
matrix(x[ind], ncol=nc)


Cheers

> On 6 Mar 2017, at 16:18, Peter Thuresson  wrote:
> 
> Hello,
> 
> Is there a function in R which can transform, let say a vector:
> 
> c(1:4)
> 
> to a matrix where the vector is repeated but "projected" +1 one step down for 
> every (new) column.
> I want the output below from the vector above, like this:
> 
> p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)
> 
> matrix(p,7,4)
> 
> best regards / Peter
> 
>   [[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] Matrix

2017-03-06 Thread Bert Gunter
Well, of course,  I *did* make a dumb error (again!!). Here's the
corrected version:

x <- c(1:5,10:12)
## generate vector of indices by outer and %%
i <- seq_along(x)
nc <- 4 ## number of columns desired
 Corrected statement 
indx <- (outer(i, rev(i-1),"+") %% length(x)) [,seq_len(nc)] +1
#  Corrected statement

matrix(x[indx],ncol = nc)


Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Mar 6, 2017 at 11:00 AM, Bert Gunter  wrote:
> Clever, Don.
>
> Here's a more explicit approach that generalizes (if I haven't made
> any dumb errors):
>
> x <- c(1:5,10:12)
> ## generate vector of indices by outer and %%
> i <- seq_along(x)
> nc <- 4 ## number of columns desired
> ## get subscripting indices via outer() and %%
> indx <- outer(i,rev(i),"+") %% (length(x))[,seq_len(nc)]+1
> matrix(x[indx],ncol = nc)
>
> Cheers,
> Bert
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Mon, Mar 6, 2017 at 9:46 AM, MacQueen, Don  wrote:
>> How about this:
>>
>> p0 <- 1:4
>>
>> matrix( c( rep( c(p0, rep(0, 4)) , times=3) , p0) , 7, 4)
>>
>> Of course, it would take some effort to generalize it to different lengths 
>> for p0.
>>
>> -Don
>>
>> --
>> Don MacQueen
>>
>> Lawrence Livermore National Laboratory
>> 7000 East Ave., L-627
>> Livermore, CA 94550
>> 925-423-1062
>>
>>
>> On 3/6/17, 8:18 AM, "R-help on behalf of Peter Thuresson" 
>>  wrote:
>>
>> Hello,
>>
>> Is there a function in R which can transform, let say a vector:
>>
>> c(1:4)
>>
>> to a matrix where the vector is repeated but "projected" +1 one step 
>> down for every (new) column.
>> I want the output below from the vector above, like this:
>>
>> p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)
>>
>> matrix(p,7,4)
>>
>> best regards / Peter
>>
>> [[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.

__
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] Matrix

2017-03-06 Thread Bert Gunter
Clever, Don.

Here's a more explicit approach that generalizes (if I haven't made
any dumb errors):

x <- c(1:5,10:12)
## generate vector of indices by outer and %%
i <- seq_along(x)
nc <- 4 ## number of columns desired
## get subscripting indices via outer() and %%
indx <- outer(i,rev(i),"+") %% (length(x))[,seq_len(nc)]+1
matrix(x[indx],ncol = nc)

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Mar 6, 2017 at 9:46 AM, MacQueen, Don  wrote:
> How about this:
>
> p0 <- 1:4
>
> matrix( c( rep( c(p0, rep(0, 4)) , times=3) , p0) , 7, 4)
>
> Of course, it would take some effort to generalize it to different lengths 
> for p0.
>
> -Don
>
> --
> Don MacQueen
>
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
>
>
> On 3/6/17, 8:18 AM, "R-help on behalf of Peter Thuresson" 
>  wrote:
>
> Hello,
>
> Is there a function in R which can transform, let say a vector:
>
> c(1:4)
>
> to a matrix where the vector is repeated but "projected" +1 one step down 
> for every (new) column.
> I want the output below from the vector above, like this:
>
> p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)
>
> matrix(p,7,4)
>
> best regards / Peter
>
> [[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.

__
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] Matrix

2017-03-06 Thread Rui Barradas

Hello,

Try the following.

proj <- function(x){
n <- length(x)
y <- numeric(n)
z <- rep(c(x, y), times = n)
z <- z[-((length(z) - n + 1):length(z))]
matrix(z, ncol = n)
}

proj(1:4)
proj(1:5)

Hope this helps,

Rui Barradas

Em 06-03-2017 16:18, Peter Thuresson escreveu:

Hello,

Is there a function in R which can transform, let say a vector:

c(1:4)

to a matrix where the vector is repeated but "projected" +1 one step down for 
every (new) column.
I want the output below from the vector above, like this:

p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)

matrix(p,7,4)

best regards / Peter

[[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] Matrix

2017-03-06 Thread MacQueen, Don
How about this:

p0 <- 1:4

matrix( c( rep( c(p0, rep(0, 4)) , times=3) , p0) , 7, 4)

Of course, it would take some effort to generalize it to different lengths for 
p0.

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062


On 3/6/17, 8:18 AM, "R-help on behalf of Peter Thuresson" 
 wrote:

Hello,

Is there a function in R which can transform, let say a vector:

c(1:4)

to a matrix where the vector is repeated but "projected" +1 one step down 
for every (new) column.
I want the output below from the vector above, like this:

p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)

matrix(p,7,4)

best regards / Peter

[[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] Matrix

2016-07-17 Thread David Winsemius

> On Jul 16, 2016, at 7:43 PM, Ashta  wrote:
> 
> HI Denes, Duncan,Michael and all,
> 
> Thank you very much  for the helpful suggestion.  Some of my data sets
> were not square matrix, however, Denes's suggestion,"
> as.data.frame.table() ", handled that one.
> 

`as.data.frame.table` should work with any matrix, not just sqaure ones:

m <- matrix(1:12, 3, 4,
   dimnames = list(dimA = letters[1:3],
   dimB = letters[1:4]))
m
#-
dimB
dimA a b c  d
   a 1 4 7 10
   b 2 5 8 11
   c 3 6 9 12
#
 as.data.frame.table(m, responseName = "value")
   dimA dimB value

1 aa 1
2 ba 2
3 ca 3
4 ab 4
5 bb 5
6 cb 6
7 ac 7
8 bc 8
9 cc 9
10ad10
11bd11
12cd12




> Thank you again.
> 
> 
> On Sat, Jul 16, 2016 at 7:27 PM, Dénes Tóth  wrote:
>> 
>> 
>> On 07/17/2016 01:39 AM, Duncan Murdoch wrote:
>>> 
>>> On 16/07/2016 6:25 PM, Ashta wrote:
 Hi all,
 
 I have a large square matrix (60 x 60)  and found it hard to
 visualize. Is it possible to change it  as shown below?
 
 Sample example (3 x 3)
 
A   B   C
 A  3   4   5
 B  4   7   8
 C  5   8   9
 
 Desired output
 A A  3
 A B  4
 A C  5
 B B  7
 B C  8
 C C  9
>>> 
>>> Yes, use matrix indexing.  I don't think the 3600 values are going to be
>>> very easy to read, but here's how to produce them:
>>> 
>>> m <- matrix(1:3600, 60, 60)
>>> indices <- expand.grid(row = 1:60, col = 1:60)
>>> cbind(indices$row, indices$col, m[as.matrix(indices)])
>>> 
>> 
>> Or use as.data.frame.table():
>> 
>> m <- matrix(1:9, 3, 3,
>>dimnames = list(dimA = letters[1:3],
>>dimB = letters[1:3]))
>> m
>> as.data.frame.table(m, responseName = "value")
>> 
>> ---
>> 
>> I do not know what you mean by "visualize", but image() or heatmap() are
>> good starting points if you need a plot of the values. If you really need to
>> inspect the raw values, you can try interactive (scrollable) tables, e.g.:
>> 
>> library(DT)
>> m <- provideDimnames(matrix(1:3600, 60, 60))
>> datatable(m, options = list(pageLength = 60))
>> 
>> 
>> Cheers,
>>  Denes
>> 
>> 
>> 
>> 
>>> Duncan Murdoch
>>> 
>>> __
>>> 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.

David Winsemius
Alameda, CA, USA

__
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] Matrix

2016-07-16 Thread Ashta
HI Denes, Duncan,Michael and all,

Thank you very much  for the helpful suggestion.  Some of my data sets
were not square matrix, however, Denes's suggestion,"
as.data.frame.table() ", handled that one.

Thank you again.


On Sat, Jul 16, 2016 at 7:27 PM, Dénes Tóth  wrote:
>
>
> On 07/17/2016 01:39 AM, Duncan Murdoch wrote:
>>
>> On 16/07/2016 6:25 PM, Ashta wrote:
>>  > Hi all,
>>  >
>>  > I have a large square matrix (60 x 60)  and found it hard to
>>  > visualize. Is it possible to change it  as shown below?
>>  >
>>  > Sample example (3 x 3)
>>  >
>>  > A   B   C
>>  > A  3   4   5
>>  > B  4   7   8
>>  > C  5   8   9
>>  >
>>  > Desired output
>>  > A A  3
>>  > A B  4
>>  > A C  5
>>  > B B  7
>>  > B C  8
>>  > C C  9
>>
>> Yes, use matrix indexing.  I don't think the 3600 values are going to be
>> very easy to read, but here's how to produce them:
>>
>> m <- matrix(1:3600, 60, 60)
>> indices <- expand.grid(row = 1:60, col = 1:60)
>> cbind(indices$row, indices$col, m[as.matrix(indices)])
>>
>
> Or use as.data.frame.table():
>
> m <- matrix(1:9, 3, 3,
> dimnames = list(dimA = letters[1:3],
> dimB = letters[1:3]))
> m
> as.data.frame.table(m, responseName = "value")
>
> ---
>
> I do not know what you mean by "visualize", but image() or heatmap() are
> good starting points if you need a plot of the values. If you really need to
> inspect the raw values, you can try interactive (scrollable) tables, e.g.:
>
> library(DT)
> m <- provideDimnames(matrix(1:3600, 60, 60))
> datatable(m, options = list(pageLength = 60))
>
>
> Cheers,
>   Denes
>
>
>
>
>> Duncan Murdoch
>>
>> __
>> 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] Matrix

2016-07-16 Thread Dénes Tóth



On 07/17/2016 01:39 AM, Duncan Murdoch wrote:

On 16/07/2016 6:25 PM, Ashta wrote:
 > Hi all,
 >
 > I have a large square matrix (60 x 60)  and found it hard to
 > visualize. Is it possible to change it  as shown below?
 >
 > Sample example (3 x 3)
 >
 > A   B   C
 > A  3   4   5
 > B  4   7   8
 > C  5   8   9
 >
 > Desired output
 > A A  3
 > A B  4
 > A C  5
 > B B  7
 > B C  8
 > C C  9

Yes, use matrix indexing.  I don't think the 3600 values are going to be
very easy to read, but here's how to produce them:

m <- matrix(1:3600, 60, 60)
indices <- expand.grid(row = 1:60, col = 1:60)
cbind(indices$row, indices$col, m[as.matrix(indices)])



Or use as.data.frame.table():

m <- matrix(1:9, 3, 3,
dimnames = list(dimA = letters[1:3],
dimB = letters[1:3]))
m
as.data.frame.table(m, responseName = "value")

---

I do not know what you mean by "visualize", but image() or heatmap() are 
good starting points if you need a plot of the values. If you really 
need to inspect the raw values, you can try interactive (scrollable) 
tables, e.g.:


library(DT)
m <- provideDimnames(matrix(1:3600, 60, 60))
datatable(m, options = list(pageLength = 60))


Cheers,
  Denes




Duncan Murdoch

__
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] Matrix

2016-07-16 Thread Michael Hannon
I'm not sure what the OP is looking for in the first two columns, but
he does seem to be looking for only the diagonal and super-diagonal
elements.  Here's some code that makes output that looks similar to
the "Desired output":

mat1 <- matrix(rbind(c(3, 4, 5),
 c(4, 7, 8),
 c(5, 8, 9)), nrow=3)
colnames(mat1) <- c('A', 'B', 'C')
rownames(mat1) <- c('A', 'B', 'C')
mat1

col3 <- mat1[row(mat1) >= col(mat1)]
idx12 <- which(row(mat1) >= col(mat1), arr.ind=TRUE)
desiredOutput <- cbind(idx12[ , 2], idx12[ , 1], col3)
desiredOutput

 If you really want 'A', 'B', 'C'
col1 <- colnames(mat1)[idx12[ , 2]]
col1
col2 <- rownames(mat1)[idx12[ , 1]]
col2

desiredOutput <- cbind(col1, col2, col3)
desiredOutput

-- Mike


On Sat, Jul 16, 2016 at 4:39 PM, Duncan Murdoch
 wrote:
> On 16/07/2016 6:25 PM, Ashta wrote:
>> Hi all,
>>
>> I have a large square matrix (60 x 60)  and found it hard to
>> visualize. Is it possible to change it  as shown below?
>>
>> Sample example (3 x 3)
>>
>> A   B   C
>> A  3   4   5
>> B  4   7   8
>> C  5   8   9
>>
>> Desired output
>> A A  3
>> A B  4
>> A C  5
>> B B  7
>> B C  8
>> C C  9
>
> Yes, use matrix indexing.  I don't think the 3600 values are going to be
> very easy to read, but here's how to produce them:
>
> m <- matrix(1:3600, 60, 60)
> indices <- expand.grid(row = 1:60, col = 1:60)
> cbind(indices$row, indices$col, m[as.matrix(indices)])
>
> Duncan Murdoch
>
>
> __
> 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] Matrix

2016-07-16 Thread Duncan Murdoch

On 16/07/2016 6:25 PM, Ashta wrote:
> Hi all,
>
> I have a large square matrix (60 x 60)  and found it hard to
> visualize. Is it possible to change it  as shown below?
>
> Sample example (3 x 3)
>
> A   B   C
> A  3   4   5
> B  4   7   8
> C  5   8   9
>
> Desired output
> A A  3
> A B  4
> A C  5
> B B  7
> B C  8
> C C  9

Yes, use matrix indexing.  I don't think the 3600 values are going to be 
very easy to read, but here's how to produce them:


m <- matrix(1:3600, 60, 60)
indices <- expand.grid(row = 1:60, col = 1:60)
cbind(indices$row, indices$col, m[as.matrix(indices)])

Duncan Murdoch

__
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] matrix indexing/subset error

2016-05-30 Thread Jeff Newmiller
z %% 2 == 1

has 12 logical values.  What do you expect R to do with it worth respect to 4 
rows? 
-- 
Sent from my phone. Please excuse my brevity.

On May 30, 2016 11:38:46 AM PDT, Carl Sutton via R-help  
wrote:
>Hi Guru's
>In my quest to understand R I have what I thought was a simple exercise
>that now has me baffled.  Why the error message after running this
>code?  I am totally baffled by the error message.  I was expecting rows
>1 and 3 of the x matrix to be returned, and have not a clue as to why
>this becomes a subscript difficulty.  The manual was of no help in this
>matter.
>
>x <- c(1:3,2:4)
>x  <- matrix(x, nrow = 3)
>x
>z <- c(1:4,1,1,0,0,1,0,1,0)
>z <- matrix(z, ncol = 3)
>z
>x[x[,2]>= 3,]
>x
>z %% 2 == 1
>x[z %% 2 == 1,]Error in x[z%%2 == 1, ] : (subscript) logical subscript
>too long
>Many thanks for your assistance.
>Carl Sutton CPA
>
>   [[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] Matrix multiplications

2016-05-21 Thread george brida
Thank you very much Peter.

On Sat, May 21, 2016 at 9:18 PM, peter dalgaard  wrote:

>
> > On 21 May 2016, at 21:00 , george brida  wrote:
> >
> > Dear R users:
> >
> > I have written the following lines :
> >
> >> x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
> >> x=matrix(x,ncol=2)
> >> a=matrix(1,nrow(x),1)
> >> X=cbind(a,x)
> >> y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75,
> 15.00)
> >
> >> b=solve(t(X)%*% X)%*% t(X)%*% y
> >
> > when I wrote the following line
> >> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> > I have obtained an error message, I don't know why namely (t(y-X %*%
> > b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
> >> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
> > [,1]
> > [1,] 3.620354
> >
> >
> > Can you please help me.
>
> Mistake is that (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) is NOT a
> scalar, but a 1 x 1 matrix.
>
> This works:
>
> as.vector((t(y-X %*% b)%*%(y-X %*% b))/(length(y)-ncol(X)))*solve(t(X)%*%
> X)
>
> as does recognizing the first term as a sum of squares:
>
> sum((y-X %*% b)^2)/(length(y)-ncol(X))*solve(t(X)%*% X)
>
> (And, for the Illuminati, a Kronecker product works too:
>
> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) %x% solve(t(X)%*% X)
>
> This could be useful for multivariate y.)
>
> >
> > Thank you
> >
> > George
> >
> >   [[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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd@cbs.dk  Priv: pda...@gmail.com
>
>
>
>
>
>
>
>
>
>

[[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] Matrix multiplications

2016-05-21 Thread peter dalgaard
I don't know if there is some sort of propagation delay, but the reason has 
already been given and multiple fixes suggested.

-pd


> On 21 May 2016, at 21:26 , george brida  wrote:
> 
> Roy,
> 
> Yes, t(y-X %*% b) is the transpose of y-X %*% b. In principle the product
> of  t(y-X %*% b) *(y-X %*% b) is a scalar, I don't know why I have an error
> message after the following line:
> 
> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> 
> Thanks
> 
> 
> On Sat, May 21, 2016 at 9:10 PM, Roy Mendelssohn - NOAA Federal <
> roy.mendelss...@noaa.gov> wrote:
> 
>>> str(t(y-X %*% b))
>> num [1, 1:10] 0.595 -1.7538 -0.0498 -1.651 -0.6328 ...
>>> str((y-X %*% b))
>> num [1:10, 1] 0.595 -1.7538 -0.0498 -1.651 -0.6328 …
>> 
>> -Roy
>> 
>> 
>>> On May 21, 2016, at 12:00 PM, george brida 
>> wrote:
>>> 
>>> Dear R users:
>>> 
>>> I have written the following lines :
>>> 
 x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
 x=matrix(x,ncol=2)
 a=matrix(1,nrow(x),1)
 X=cbind(a,x)
 y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75,
>> 15.00)
>>> 
 b=solve(t(X)%*% X)%*% t(X)%*% y
>>> 
>>> when I wrote the following line
 (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
>>> I have obtained an error message, I don't know why namely (t(y-X %*%
>>> b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
 (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
>>>[,1]
>>> [1,] 3.620354
>>> 
>>> 
>>> Can you please help me.
>>> 
>>> Thank you
>>> 
>>> George
>>> 
>>>  [[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.
>> 
>> **
>> "The contents of this message do not reflect any position of the U.S.
>> Government or NOAA."
>> **
>> Roy Mendelssohn
>> Supervisory Operations Research Analyst
>> NOAA/NMFS
>> Environmental Research Division
>> Southwest Fisheries Science Center
>> ***Note new address and phone***
>> 110 Shaffer Road
>> Santa Cruz, CA 95060
>> Phone: (831)-420-3666
>> Fax: (831) 420-3980
>> e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/
>> 
>> "Old age and treachery will overcome youth and skill."
>> "From those who have been given much, much will be expected"
>> "the arc of the moral universe is long, but it bends toward justice" -MLK
>> Jr.
>> 
>> 
> 
>   [[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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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] Matrix multiplications

2016-05-21 Thread george brida
Roy,

Yes, t(y-X %*% b) is the transpose of y-X %*% b. In principle the product
of  t(y-X %*% b) *(y-X %*% b) is a scalar, I don't know why I have an error
message after the following line:

(t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)

Thanks


On Sat, May 21, 2016 at 9:10 PM, Roy Mendelssohn - NOAA Federal <
roy.mendelss...@noaa.gov> wrote:

> >  str(t(y-X %*% b))
>  num [1, 1:10] 0.595 -1.7538 -0.0498 -1.651 -0.6328 ...
> > str((y-X %*% b))
>  num [1:10, 1] 0.595 -1.7538 -0.0498 -1.651 -0.6328 …
>
> -Roy
>
>
> > On May 21, 2016, at 12:00 PM, george brida 
> wrote:
> >
> > Dear R users:
> >
> > I have written the following lines :
> >
> >> x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
> >> x=matrix(x,ncol=2)
> >> a=matrix(1,nrow(x),1)
> >> X=cbind(a,x)
> >> y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75,
> 15.00)
> >
> >> b=solve(t(X)%*% X)%*% t(X)%*% y
> >
> > when I wrote the following line
> >> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> > I have obtained an error message, I don't know why namely (t(y-X %*%
> > b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
> >> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
> > [,1]
> > [1,] 3.620354
> >
> >
> > Can you please help me.
> >
> > Thank you
> >
> > George
> >
> >   [[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.
>
> **
> "The contents of this message do not reflect any position of the U.S.
> Government or NOAA."
> **
> Roy Mendelssohn
> Supervisory Operations Research Analyst
> NOAA/NMFS
> Environmental Research Division
> Southwest Fisheries Science Center
> ***Note new address and phone***
> 110 Shaffer Road
> Santa Cruz, CA 95060
> Phone: (831)-420-3666
> Fax: (831) 420-3980
> e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/
>
> "Old age and treachery will overcome youth and skill."
> "From those who have been given much, much will be expected"
> "the arc of the moral universe is long, but it bends toward justice" -MLK
> Jr.
>
>

[[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] Matrix multiplications

2016-05-21 Thread peter dalgaard

> On 21 May 2016, at 21:00 , george brida  wrote:
> 
> Dear R users:
> 
> I have written the following lines :
> 
>> x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
>> x=matrix(x,ncol=2)
>> a=matrix(1,nrow(x),1)
>> X=cbind(a,x)
>> y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75, 15.00)
> 
>> b=solve(t(X)%*% X)%*% t(X)%*% y
> 
> when I wrote the following line
>> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> I have obtained an error message, I don't know why namely (t(y-X %*%
> b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
>> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
> [,1]
> [1,] 3.620354
> 
> 
> Can you please help me.

Mistake is that (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) is NOT a 
scalar, but a 1 x 1 matrix. 

This works:

as.vector((t(y-X %*% b)%*%(y-X %*% b))/(length(y)-ncol(X)))*solve(t(X)%*% X)

as does recognizing the first term as a sum of squares:

sum((y-X %*% b)^2)/(length(y)-ncol(X))*solve(t(X)%*% X)

(And, for the Illuminati, a Kronecker product works too:

(t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) %x% solve(t(X)%*% X)

This could be useful for multivariate y.)

> 
> Thank you
> 
> George
> 
>   [[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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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] Matrix multiplications

2016-05-21 Thread Roy Mendelssohn - NOAA Federal
>  str(t(y-X %*% b))
 num [1, 1:10] 0.595 -1.7538 -0.0498 -1.651 -0.6328 ...
> str((y-X %*% b))
 num [1:10, 1] 0.595 -1.7538 -0.0498 -1.651 -0.6328 …

-Roy


> On May 21, 2016, at 12:00 PM, george brida  wrote:
> 
> Dear R users:
> 
> I have written the following lines :
> 
>> x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
>> x=matrix(x,ncol=2)
>> a=matrix(1,nrow(x),1)
>> X=cbind(a,x)
>> y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75, 15.00)
> 
>> b=solve(t(X)%*% X)%*% t(X)%*% y
> 
> when I wrote the following line
>> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> I have obtained an error message, I don't know why namely (t(y-X %*%
> b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
>> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
> [,1]
> [1,] 3.620354
> 
> 
> Can you please help me.
> 
> Thank you
> 
> George
> 
>   [[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.

**
"The contents of this message do not reflect any position of the U.S. 
Government or NOAA."
**
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new address and phone***
110 Shaffer Road
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

__
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] Matrix: How create a _row-oriented_ sparse Matrix (=dgRMatrix)?

2016-04-20 Thread Henrik Bengtsson
On Wed, Apr 20, 2016 at 1:25 AM, Martin Maechler
 wrote:
>> Henrik Bengtsson 
>> on Tue, 19 Apr 2016 14:04:11 -0700 writes:
>
> > Using the Matrix package, how can I create a row-oriented sparse
> > Matrix from scratch populated with some data?  By default a
> > column-oriented one is created and I'm aware of the note that the
> > package is optimized for column-oriented ones, but I'm only interested
> > in using it for holding my sparse row-oriented data and doing basic
> > subsetting by rows (even using drop=FALSE).
>
> > Here is what I get when I set up a column-oriented sparse Matrix:
>
> >> Cc <- Matrix(0, nrow=5, ncol=5, sparse=TRUE)
> >> Cc[1:3,1] <- 1
>
> A general ("teaching") remark :
> The above use of Matrix() is seen in many places, and is fine
> for small matrices and the case where you only use the `[<-`
> method very few times (as above).
> Also using  Matrix()  is nice when being introduced to using the
> Matrix package.
>
> However, for efficience in non-small cases, do use
>
>sparseMatrix()
>
> directly to construct sparse matrices.
>
>
> >> Cc
> > 5 x 5 sparse Matrix of class "dgCMatrix"
>
> > [1,] 1 . . . .
> > [2,] 1 . . . .
> > [3,] 1 . . . .
> > [4,] . . . . .
> > [5,] . . . . .
> >> str(Cc)
> > Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
> > ..@ i   : int [1:3] 0 1 2
> > ..@ p   : int [1:6] 0 3 3 3 3 3
> > ..@ Dim : int [1:2] 5 5
> > ..@ Dimnames:List of 2
> > .. ..$ : NULL
> > .. ..$ : NULL
> > ..@ x   : num [1:3] 1 1 1
> > ..@ factors : list()
>
> > When I try to do the analogue for a row-oriented matrix, I get a
> > "dgTMatrix", whereas I would expect a "dgRMatrix":
>
> >> Cr <- Matrix(0, nrow=5, ncol=5, sparse=TRUE)
> >> Cr <- as(Cr, "dsRMatrix")
> >> Cr[1,1:3] <- 1
> >> Cr
> > 5 x 5 sparse Matrix of class "dgTMatrix"
>
> > [1,] 1 1 1 . .
> > [2,] . . . . .
> > [3,] . . . . .
> > [4,] . . . . .
> > [5,] . . . . .
>
> The reason for the above behavior has been
>
> a) efficiency.  All the subassignment ( `[<-` ) methods for
>"RsparseMatrix" objects (of which "dsRMatrix" is a special case)
>are implemented via  TsparseMatrix.
> b) because of the general attitude that Csparse (and Tsparse to
>some extent) are well supported in Matrix,
>and e.g. further operations on Rsparse matrices would *again*
>go via T* or C* sparse ones, I had decided to keep things Tsparse.

Thanks, understanding these design decisions is helpful.
Particularly, since I consider myself a rookie when it comes to the
Matrix package.

>
> [...]
>
> > Trying with explicit coercion does not work:
>
> >> as(Cc, "dgRMatrix")
> > Error in as(Cc, "dgRMatrix") :
> > no method or default for coercing "dgCMatrix" to "dgRMatrix"
>
> >> as(Cr, "dgRMatrix")
> > Error in as(Cr, "dgRMatrix") :
> > no method or default for coercing "dgTMatrix" to "dgRMatrix"
>
> The general philosophy in 'Matrix' with all the class
> hierarchies and the many specific classes has been to allow and
> foster coercing to abstract super classes,
> i.e, to  "dMatrix"  or "generalMatrix", "triangularMatrix", or
> then "denseMatrix", "sparseMatrix", "CsparseMatrix" or
> "RsparseMatrix", etc
>
> So in the above  as(*, "RsparseMatrix")   should work always.

Thanks for pointing this out (and confirming as I since discovered the
virtual RsparseMatrix class in the help).

>
>
> As a summary, in other words,  for what you want,
>
>as(sparseMatrix(.), "RsparseMatrix")
>
> should give you what you want reliably and efficiently.

Perfect.

>
>
> > Am I doing some wrong here?  Or is this what means that the package is
> > optimized for the column-oriented representation and I shouldn't
> > really work with row-oriented ones?  I'm really only interested in
> > access to efficient Cr[row,,drop=FALSE] subsetting (and a small memory
> > footprint).
>
> { though you could equivalently use   Cc[,row, drop=FALSE]
>   with a CsparseMatrix Cc := t(Cr),
>   couldn't you ?
> }

Yes, I actually went ahead did that, but since the code I'm writing
supports both plain matrix:es and sparse Matrix:es, and the underlying
model operates row-by-row, I figured the code would be more consistent
if I could use row-orientation everywhere.  Not a big deal.

Thanks Martin

Henrik

>
>
> Martin Maechler  (maintainer of 'Matrix')
> ETH Zurich
>

__
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] Matrix: How create a _row-oriented_ sparse Matrix (=dgRMatrix)?

2016-04-20 Thread Martin Maechler
> Henrik Bengtsson 
> on Tue, 19 Apr 2016 14:04:11 -0700 writes:

> Using the Matrix package, how can I create a row-oriented sparse
> Matrix from scratch populated with some data?  By default a
> column-oriented one is created and I'm aware of the note that the
> package is optimized for column-oriented ones, but I'm only interested
> in using it for holding my sparse row-oriented data and doing basic
> subsetting by rows (even using drop=FALSE).

> Here is what I get when I set up a column-oriented sparse Matrix:

>> Cc <- Matrix(0, nrow=5, ncol=5, sparse=TRUE)
>> Cc[1:3,1] <- 1

A general ("teaching") remark :
The above use of Matrix() is seen in many places, and is fine
for small matrices and the case where you only use the `[<-`
method very few times (as above).
Also using  Matrix()  is nice when being introduced to using the
Matrix package.

However, for efficience in non-small cases, do use

   sparseMatrix()

directly to construct sparse matrices.


>> Cc
> 5 x 5 sparse Matrix of class "dgCMatrix"

> [1,] 1 . . . .
> [2,] 1 . . . .
> [3,] 1 . . . .
> [4,] . . . . .
> [5,] . . . . .
>> str(Cc)
> Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
> ..@ i   : int [1:3] 0 1 2
> ..@ p   : int [1:6] 0 3 3 3 3 3
> ..@ Dim : int [1:2] 5 5
> ..@ Dimnames:List of 2
> .. ..$ : NULL
> .. ..$ : NULL
> ..@ x   : num [1:3] 1 1 1
> ..@ factors : list()

> When I try to do the analogue for a row-oriented matrix, I get a
> "dgTMatrix", whereas I would expect a "dgRMatrix":

>> Cr <- Matrix(0, nrow=5, ncol=5, sparse=TRUE)
>> Cr <- as(Cr, "dsRMatrix")
>> Cr[1,1:3] <- 1
>> Cr
> 5 x 5 sparse Matrix of class "dgTMatrix"

> [1,] 1 1 1 . .
> [2,] . . . . .
> [3,] . . . . .
> [4,] . . . . .
> [5,] . . . . .

The reason for the above behavior has been

a) efficiency.  All the subassignment ( `[<-` ) methods for
   "RsparseMatrix" objects (of which "dsRMatrix" is a special case)
   are implemented via  TsparseMatrix.
b) because of the general attitude that Csparse (and Tsparse to
   some extent) are well supported in Matrix,
   and e.g. further operations on Rsparse matrices would *again*
   go via T* or C* sparse ones, I had decided to keep things Tsparse.

[...]

> Trying with explicit coercion does not work:

>> as(Cc, "dgRMatrix")
> Error in as(Cc, "dgRMatrix") :
> no method or default for coercing "dgCMatrix" to "dgRMatrix"

>> as(Cr, "dgRMatrix")
> Error in as(Cr, "dgRMatrix") :
> no method or default for coercing "dgTMatrix" to "dgRMatrix"

The general philosophy in 'Matrix' with all the class
hierarchies and the many specific classes has been to allow and
foster coercing to abstract super classes,
i.e, to  "dMatrix"  or "generalMatrix", "triangularMatrix", or
then "denseMatrix", "sparseMatrix", "CsparseMatrix" or
"RsparseMatrix", etc

So in the above  as(*, "RsparseMatrix")   should work always.


As a summary, in other words,  for what you want,

   as(sparseMatrix(.), "RsparseMatrix")

should give you what you want reliably and efficiently.


> Am I doing some wrong here?  Or is this what means that the package is
> optimized for the column-oriented representation and I shouldn't
> really work with row-oriented ones?  I'm really only interested in
> access to efficient Cr[row,,drop=FALSE] subsetting (and a small memory
> footprint).

{ though you could equivalently use   Cc[,row, drop=FALSE]
  with a CsparseMatrix Cc := t(Cr),
  couldn't you ?
}


Martin Maechler  (maintainer of 'Matrix')
ETH Zurich

__
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] Matrix summary

2016-02-12 Thread Jim Lemon
Hi Ashta,
Surely you are aware of the "apply" family of functions that return the
numbers you want:

ashmat<-matrix(c(117,12,13,21,21,32,11,1,65,43,23,7,58,61,78,95 ),
 nrow=4,byrow=TRUE)
apply(ashmat,2,mean)
[1] 65.25 37.00 31.25 31.00
apply(ashmat,1,which.max)
[1] 1 2 1 4

Jim


On Sat, Feb 13, 2016 at 3:04 PM, Ashta  wrote:

> hi all,
>
> I have  a square matrix (1000 by 1000),
> 1. I want calculate  mean,  min and max values for each column and row.
>
> 2, I want pick the  coordinate value of the matrix that has the max
> and min value for each row and column.
> This an example 4 by 4 square matrix
>
>
>   MeanMinMax
> 117   1213 2140.75   12117
>  213211   1 16.25   1   32
>  654323   7  34.57   65
>  586178 957358  95
> Mean652537   31.25
> Min2112111
> Max 117617895
>
>
> Thank you
>
> __
> 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] Matrix summary

2016-02-12 Thread Bert Gunter
Yes, but colMeans, rowMeans, pmax, pmin , etc. are *much* faster.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Feb 12, 2016 at 9:15 PM, Jim Lemon  wrote:
> Hi Ashta,
> Surely you are aware of the "apply" family of functions that return the
> numbers you want:
>
> ashmat<-matrix(c(117,12,13,21,21,32,11,1,65,43,23,7,58,61,78,95 ),
>  nrow=4,byrow=TRUE)
> apply(ashmat,2,mean)
> [1] 65.25 37.00 31.25 31.00
> apply(ashmat,1,which.max)
> [1] 1 2 1 4
>
> Jim
>
>
> On Sat, Feb 13, 2016 at 3:04 PM, Ashta  wrote:
>
>> hi all,
>>
>> I have  a square matrix (1000 by 1000),
>> 1. I want calculate  mean,  min and max values for each column and row.
>>
>> 2, I want pick the  coordinate value of the matrix that has the max
>> and min value for each row and column.
>> This an example 4 by 4 square matrix
>>
>>
>>   MeanMinMax
>> 117   1213 2140.75   12117
>>  213211   1 16.25   1   32
>>  654323   7  34.57   65
>>  586178 957358  95
>> Mean652537   31.25
>> Min2112111
>> Max 117617895
>>
>>
>> Thank you
>>
>> __
>> 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-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] Matrix of Lists containing numbers and characters

2016-01-25 Thread PIKAL Petr
Hi

What is Temp?

Just a guess. "model" variable is factor and it is converted to its numeric 
representation during paste or any other operation you made.

Cheers
Petr

> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of TJUN
> KIAT TEO
> Sent: Monday, January 25, 2016 9:53 AM
> To: r-help@r-project.org
> Subject: [R] Matrix of Lists containing numbers and characters
>
> Here is my sample code
>
> TunePar<-matrix(list(Null),2,2)
>
> TunePar[[1,1]]=list(subclasses=3,model="gen.ridge")
>
> tune=paste(colnames(Temp),Temp,sep="=")
> tune=paste(tune,collapse=",")
>
> However when I type tune
>
> This is what I get
>
>  "subclasses=3,model=1"
>
>
> The text "gen.ridge has been converted to the number 1. Any idea why?
> Thanks
>
>
>
>
>   [[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.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
__
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] matrix which results singular but at the same time positive definite

2015-12-28 Thread Stefano Sofia
Dear Dr.Gilbert,

it took me a bit of time to understand your thoughtful comment.
You are right on everything. I was not able to see it, and likely I still have 
something to understand better some consequences on what I am trying to do.

Thank you
Stefano


Da: Paul Gilbert [pgilbert...@gmail.com]
Inviato: martedì 15 dicembre 2015 15.28
A: Stefano Sofia
Cc: r-help@r-project.org; Fox, John; peter dalgaard
Oggetto: Re: [R] matrix which results singular but at the same time positive 
definite

Stefano

I think in other response to in this thread you got the answer to the
question you asked, but you may be asking the wrong question. I'm not
familiar with the specific papers you mention and you have not provided
enough detail about what you are doing, so I am guessing a bit. The term
"dynamic linear model" can refer to both linear ARMA/ARIMA models and to
linear state-space models, however some authors use it to refer
exclusively to state-space models and from your phrasing I am guessing
you are doing that. There would be many state-space models equivalent to
a given ARMA/ARIMA model, but without specifying structural aspects of
the system you will likely be using one of the innovations form
state-space models that are equivalent. In an innovations form
state-space model the state is defined as an expectation. From a
practical point of view, this is one of the most important differences
between an innovation form and a non-innovations form state-space model.
Since the expectation is known exactly, the state-tracking error is
zero. That means the covariance matrix from the filter or smoother
should be a zero matrix, which you should not be trying to invert. In a
non-innovations form the state has a physical interpretation rather than
being an expectation, so the state tracking error should not be
degenerate in that case.

I mention all this because your covariance matrix looks very close to zero.

Paul Gilbert

On 12/11/2015 06:00 AM, r-help-requ...@r-project.org wrote:
> Dear John, thank you for your considerations. This matrix (which is a
> variance matrix) is part of an algorithm for forward-filtering and
> backward-sampling of Dynamic Linear Models (West and Harrison, 1997),
> applied to DLM representation of ARIMA processes (Petris, Petrone,
> Campagnoli).  It is therefore very difficult to explain why this
> variance matrix becomes so ill conditioned. This already happens at
> the first iteration of the algorithm. I will try to work on initial
> conditions and some fixed parameters.
>
> Thank you again Stefano
>



AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere 
informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si 
è il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al 
mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi 
dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed 
urgenza, la risposta al presente messaggio di posta elettronica può essere 
visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by 
persons entitled to receive the confidential information it may contain. E-mail 
messages to clients of Regione Marche may contain information that is 
confidential and legally privileged. Please do not read, copy, forward, or 
store this message unless you are an intended recipient of it. If you have 
received this message in error, please forward it to the sender and delete it 
completely from your computer system.
__
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] matrix which results singular but at the same time positive definite

2015-12-15 Thread Paul Gilbert

Stefano

I think in other response to in this thread you got the answer to the 
question you asked, but you may be asking the wrong question. I'm not 
familiar with the specific papers you mention and you have not provided 
enough detail about what you are doing, so I am guessing a bit. The term 
"dynamic linear model" can refer to both linear ARMA/ARIMA models and to 
linear state-space models, however some authors use it to refer 
exclusively to state-space models and from your phrasing I am guessing 
you are doing that. There would be many state-space models equivalent to 
a given ARMA/ARIMA model, but without specifying structural aspects of 
the system you will likely be using one of the innovations form 
state-space models that are equivalent. In an innovations form 
state-space model the state is defined as an expectation. From a 
practical point of view, this is one of the most important differences 
between an innovation form and a non-innovations form state-space model. 
Since the expectation is known exactly, the state-tracking error is 
zero. That means the covariance matrix from the filter or smoother 
should be a zero matrix, which you should not be trying to invert. In a 
non-innovations form the state has a physical interpretation rather than 
being an expectation, so the state tracking error should not be 
degenerate in that case.


I mention all this because your covariance matrix looks very close to zero.

Paul Gilbert

On 12/11/2015 06:00 AM, r-help-requ...@r-project.org wrote:

Dear John, thank you for your considerations. This matrix (which is a
variance matrix) is part of an algorithm for forward-filtering and
backward-sampling of Dynamic Linear Models (West and Harrison, 1997),
applied to DLM representation of ARIMA processes (Petris, Petrone,
Campagnoli).  It is therefore very difficult to explain why this
variance matrix becomes so ill conditioned. This already happens at
the first iteration of the algorithm. I will try to work on initial
conditions and some fixed parameters.

Thank you again Stefano



__
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] matrix which results singular but at the same time positive definite

2015-12-14 Thread Stefano Sofia
Dear John and dear Peter,
I needed time to understand better some practical implications derived from 
your hints.

Thank you
Stefano


Da: Fox, John [j...@mcmaster.ca]
Inviato: giovedì 10 dicembre 2015 17.24
A: peter dalgaard; Stefano Sofia
Cc: r-help@r-project.org
Oggetto: RE: [R] matrix which results singular but at the same time positive 
definite

Dear Peter,

> -Original Message-
> From: peter dalgaard [mailto:pda...@gmail.com]
> Sent: Thursday, December 10, 2015 11:09 AM
> To: Stefano Sofia
> Cc: Fox, John; r-help@r-project.org
> Subject: Re: [R] matrix which results singular but at the same time
> positive definite
>
> Looks like the ill-conditioning is almost entirely due to scaling, e.g.

Yes, that's my point. Sorry I didn't make it clearer.

Best,
 John

>
> > eigen(cov2cor(m))
> $values
> [1] 1.7234899 1.3380701 0.6619299 0.2765101
> ...
>
> This is an annoyance in several parts of numerical linear algebra:
> Routines assume that R^n has all coordinates on a similar scale and
> therefore think that anything on the order of 1e-7 or so is effectively
> zero.
>
> Condition numbers do this too:
>
> > kappa(m)
> [1] 1.066582e+13
> > kappa(cov2cor(m))
> [1] 5.489243
>
>
> -pd
>
> On 10 Dec 2015, at 16:41 , Stefano Sofia
> <stefano.so...@regione.marche.it> wrote:
>
> > Dear John,
> > thank you for your considerations.
> > This matrix (which is a variance matrix) is part of an algorithm for
> forward-filtering and backward-sampling of Dynamic Linear Models (West
> and Harrison, 1997), applied to DLM representation of ARIMA processes
> (Petris, Petrone, Campagnoli).  It is therefore very difficult to
> explain why this variance matrix becomes so ill conditioned. This
> already happens at the first iteration of the algorithm. I will try to
> work on initial conditions and some fixed parameters.
> >
> > Thank you again
> > Stefano
> >
> >
> > 
> > Da: Fox, John [j...@mcmaster.ca]
> > Inviato: giovedì 10 dicembre 2015 14.41
> > A: Stefano Sofia; r-help@r-project.org
> > Oggetto: RE: matrix which results singular but at the same time
> positivedefinite
> >
> > Dear Stefano,
> >
> > You've already had a couple of informative responses directly
> addressing your question, but are you aware how ill-conditioned the
> matrix is (one of the responses alluded to this)?
> >
> >> kappa(X, exact=TRUE)
> > [1] 7.313338e+12
> >
> >> eigen(X)$values
> > [1] 4.964711e+00 9.356881e-01 4.863392e-12 6.788344e-13
> >
> > Two of the variables have variances around 10^0 and the other two
> around 10^-12. Of course, you haven't said anything about the context,
> but does it really make sense to analyze the data on these scales?
> >
> > Best,
> > John
> >
> > -
> > John Fox, Professor
> > McMaster University
> > Hamilton, Ontario
> > Canada L8S 4M4
> > Web: socserv.mcmaster.ca/jfox
> >
> >
> >
> >
> >> -Original Message-
> >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of
> Stefano Sofia
> >> Sent: December 10, 2015 5:08 AM
> >> To: r-help@r-project.org
> >> Subject: [R] matrix which results singular but at the same time
> positive definite
> >>
> >> Dear list users,
> >> through the "matrixcalc" package I am performing some checks of
> variance
> >> matrices (which must be positive definite).
> >> In this example, it happens that the matrix A here reported is
> singular but
> >> positive definite. Is it possible?
> >>
> >>  [,1]  [,2]  [,3]  [,4]
> >> [1,]  1.904255e-12 -1.904255e-12 -8.238960e-13 -1.240294e-12 [2,] -
> >> 1.904255e-12  3.637979e-12  1.364242e-12  1.818989e-12 [3,] -
> 8.238960e-13
> >> 1.364242e-12  4.809988e+00  7.742369e-01 [4,] -1.240294e-12
> 1.818989e-12
> >> 7.742369e-01  1.090411e+00
> >>
> >> print(is.non.singular.matrix(A, tol = 1e-18)) FALSE
> print(is.positive.definite(A,
> >> tol=1e-18)) TRUE
> >>
> >> Is there something wrong with this matrix?
> >> Any comment will be appreciated.
> >> Stefano
> >>
> >>
> >> 
> >>
> >> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può
> contenere
> >> informazioni confidenziali, pertanto è destinato solo a persone
> autorizzate alla
> >> rice

Re: [R] matrix which results singular but at the same time positive definite

2015-12-10 Thread Giorgio Garziano
Decrease the "tol" parameter specified into the "is.non.singular.matrix() call,
for example as:

m <- matrix(c( 1.904255e-12, -1.904255e-12, -8.238960e-13, -1.240294e-12,
   -1.904255e-12,  3.637979e-12,  1.364242e-12,  1.818989e-12,
   -8.238960e-13,  1.364242e-12,  4.809988e+00,  7.742369e-01,
   -1.240294e-12,  1.818989e-12,  7.742369e-01,  1.090411e+00),
nrow=4, ncol=4)


> m

  [,1]  [,2]  [,3]  [,4]

[1,]  1.904255e-12 -1.904255e-12 -8.238960e-13 -1.240294e-12

[2,] -1.904255e-12  3.637979e-12  1.364242e-12  1.818989e-12

[3,] -8.238960e-13  1.364242e-12  4.809988e+00  7.742369e-01

[4,] -1.240294e-12  1.818989e-12  7.742369e-01  1.090411e+00


> print(is.non.singular.matrix(m, tol = 1e-24))
[1] TRUE

> print(is.positive.definite(m, tol=1e-18))
[1] TRUE


--

GG

http://around-r.blogspot.it





[[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] matrix which results singular but at the same time positive definite

2015-12-10 Thread Rolf Turner

On 10/12/15 23:08, Stefano Sofia wrote:

Dear list users,
through the "matrixcalc" package I am performing some checks of variance 
matrices (which must be positive definite).
In this example, it happens that the matrix A here reported is singular but 
positive definite. Is it possible?

   [,1]  [,2]  [,3]  [,4]
[1,]  1.904255e-12 -1.904255e-12 -8.238960e-13 -1.240294e-12
[2,] -1.904255e-12  3.637979e-12  1.364242e-12  1.818989e-12
[3,] -8.238960e-13  1.364242e-12  4.809988e+00  7.742369e-01
[4,] -1.240294e-12  1.818989e-12  7.742369e-01  1.090411e+00

print(is.non.singular.matrix(A, tol = 1e-18))
FALSE
print(is.positive.definite(A, tol=1e-18))
TRUE

Is there something wrong with this matrix?
Any comment will be appreciated.


There is nothing wrong with A (at least nothing that either a nice bowl 
of chicken soup or a bloody good swim wouldn't cure).


Look at the code for the two functions.  The tests use the tolerance in 
very different ways.


My initial reaction is that the code for these functions is rather naive.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
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] matrix which results singular but at the same time positive definite

2015-12-10 Thread Fox, John
Dear Stefano,

You've already had a couple of informative responses directly addressing your 
question, but are you aware how ill-conditioned the matrix is (one of the 
responses alluded to this)?

> kappa(X, exact=TRUE)
[1] 7.313338e+12

> eigen(X)$values
[1] 4.964711e+00 9.356881e-01 4.863392e-12 6.788344e-13

Two of the variables have variances around 10^0 and the other two around 
10^-12. Of course, you haven't said anything about the context, but does it 
really make sense to analyze the data on these scales?

Best,
 John

-
John Fox, Professor
McMaster University
Hamilton, Ontario
Canada L8S 4M4
Web: socserv.mcmaster.ca/jfox




> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Stefano Sofia
> Sent: December 10, 2015 5:08 AM
> To: r-help@r-project.org
> Subject: [R] matrix which results singular but at the same time positive 
> definite
> 
> Dear list users,
> through the "matrixcalc" package I am performing some checks of variance
> matrices (which must be positive definite).
> In this example, it happens that the matrix A here reported is singular but
> positive definite. Is it possible?
> 
>   [,1]  [,2]  [,3]  [,4]
> [1,]  1.904255e-12 -1.904255e-12 -8.238960e-13 -1.240294e-12 [2,] -
> 1.904255e-12  3.637979e-12  1.364242e-12  1.818989e-12 [3,] -8.238960e-13
> 1.364242e-12  4.809988e+00  7.742369e-01 [4,] -1.240294e-12  1.818989e-12
> 7.742369e-01  1.090411e+00
> 
> print(is.non.singular.matrix(A, tol = 1e-18)) FALSE 
> print(is.positive.definite(A,
> tol=1e-18)) TRUE
> 
> Is there something wrong with this matrix?
> Any comment will be appreciated.
> Stefano
> 
> 
> 
> 
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere
> informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
> alla
> ricezione. I messaggi di posta elettronica per i client di Regione Marche
> possono contenere informazioni confidenziali e con privilegi legali. Se non 
> si è il
> destinatario specificato, non leggere, copiare, inoltrare o archiviare questo
> messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al 
> mittente
> ed eliminarlo completamente dal sistema del proprio computer. Ai sensi
> dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed
> urgenza, la risposta al presente messaggio di posta elettronica può essere
> visionata da persone estranee al destinatario.
> IMPORTANT NOTICE: This e-mail message is intended to be received only by
> persons entitled to receive the confidential information it may contain. 
> E-mail
> messages to clients of Regione Marche may contain information that is
> confidential and legally privileged. Please do not read, copy, forward, or 
> store
> this message unless you are an intended recipient of it. If you have received
> this message in error, please forward it to the sender and delete it 
> completely
> from your computer system.
> 
>   [[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] matrix which results singular but at the same time positive definite

2015-12-10 Thread Stefano Sofia
Dear John,
thank you for your considerations.
This matrix (which is a variance matrix) is part of an algorithm for 
forward-filtering and backward-sampling of Dynamic Linear Models (West and 
Harrison, 1997), applied to DLM representation of ARIMA processes (Petris, 
Petrone, Campagnoli).  It is therefore very difficult to explain why this 
variance matrix becomes so ill conditioned. This already happens at the first 
iteration of the algorithm. I will try to work on initial conditions and some 
fixed parameters.

Thank you again
Stefano



Da: Fox, John [j...@mcmaster.ca]
Inviato: giovedì 10 dicembre 2015 14.41
A: Stefano Sofia; r-help@r-project.org
Oggetto: RE: matrix which results singular but at the same time positive
definite

Dear Stefano,

You've already had a couple of informative responses directly addressing your 
question, but are you aware how ill-conditioned the matrix is (one of the 
responses alluded to this)?

> kappa(X, exact=TRUE)
[1] 7.313338e+12

> eigen(X)$values
[1] 4.964711e+00 9.356881e-01 4.863392e-12 6.788344e-13

Two of the variables have variances around 10^0 and the other two around 
10^-12. Of course, you haven't said anything about the context, but does it 
really make sense to analyze the data on these scales?

Best,
 John

-
John Fox, Professor
McMaster University
Hamilton, Ontario
Canada L8S 4M4
Web: socserv.mcmaster.ca/jfox




> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Stefano Sofia
> Sent: December 10, 2015 5:08 AM
> To: r-help@r-project.org
> Subject: [R] matrix which results singular but at the same time positive 
> definite
>
> Dear list users,
> through the "matrixcalc" package I am performing some checks of variance
> matrices (which must be positive definite).
> In this example, it happens that the matrix A here reported is singular but
> positive definite. Is it possible?
>
>   [,1]  [,2]  [,3]  [,4]
> [1,]  1.904255e-12 -1.904255e-12 -8.238960e-13 -1.240294e-12 [2,] -
> 1.904255e-12  3.637979e-12  1.364242e-12  1.818989e-12 [3,] -8.238960e-13
> 1.364242e-12  4.809988e+00  7.742369e-01 [4,] -1.240294e-12  1.818989e-12
> 7.742369e-01  1.090411e+00
>
> print(is.non.singular.matrix(A, tol = 1e-18)) FALSE 
> print(is.positive.definite(A,
> tol=1e-18)) TRUE
>
> Is there something wrong with this matrix?
> Any comment will be appreciated.
> Stefano
>
>
> 
>
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere
> informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
> alla
> ricezione. I messaggi di posta elettronica per i client di Regione Marche
> possono contenere informazioni confidenziali e con privilegi legali. Se non 
> si è il
> destinatario specificato, non leggere, copiare, inoltrare o archiviare questo
> messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al 
> mittente
> ed eliminarlo completamente dal sistema del proprio computer. Ai sensi
> dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed
> urgenza, la risposta al presente messaggio di posta elettronica può essere
> visionata da persone estranee al destinatario.
> IMPORTANT NOTICE: This e-mail message is intended to be received only by
> persons entitled to receive the confidential information it may contain. 
> E-mail
> messages to clients of Regione Marche may contain information that is
> confidential and legally privileged. Please do not read, copy, forward, or 
> store
> this message unless you are an intended recipient of it. If you have received
> this message in error, please forward it to the sender and delete it 
> completely
> from your computer system.
>
>   [[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.



AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere 
informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si 
è il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al 
mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi 
dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed 
urgenza, la risposta al presente messaggio di posta elettronica può essere 
visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is 

Re: [R] matrix which results singular but at the same time positive definite

2015-12-10 Thread Fox, John
Dear Stefano,

I don't really know anything about your application, but my point is about the 
scaling of the variables. Can't you rescale some or all of the variables so 
their variances aren't so different? For example, the correlation matrix among 
these four variables isn't ill-conditioned.

Best,
 John

> -Original Message-
> From: Stefano Sofia [mailto:stefano.so...@regione.marche.it]
> Sent: Thursday, December 10, 2015 10:42 AM
> To: Fox, John; r-help@r-project.org
> Subject: RE: matrix which results singular but at the same time positive
> definite
> 
> Dear John,
> thank you for your considerations.
> This matrix (which is a variance matrix) is part of an algorithm for
> forward-filtering and backward-sampling of Dynamic Linear Models (West
> and Harrison, 1997), applied to DLM representation of ARIMA processes
> (Petris, Petrone, Campagnoli).  It is therefore very difficult to
> explain why this variance matrix becomes so ill conditioned. This
> already happens at the first iteration of the algorithm. I will try to
> work on initial conditions and some fixed parameters.
> 
> Thank you again
> Stefano
> 
> 
> 
> Da: Fox, John [j...@mcmaster.ca]
> Inviato: giovedì 10 dicembre 2015 14.41
> A: Stefano Sofia; r-help@r-project.org
> Oggetto: RE: matrix which results singular but at the same time positive
> definite
> 
> Dear Stefano,
> 
> You've already had a couple of informative responses directly addressing
> your question, but are you aware how ill-conditioned the matrix is (one
> of the responses alluded to this)?
> 
> > kappa(X, exact=TRUE)
> [1] 7.313338e+12
> 
> > eigen(X)$values
> [1] 4.964711e+00 9.356881e-01 4.863392e-12 6.788344e-13
> 
> Two of the variables have variances around 10^0 and the other two around
> 10^-12. Of course, you haven't said anything about the context, but does
> it really make sense to analyze the data on these scales?
> 
> Best,
>  John
> 
> -
> John Fox, Professor
> McMaster University
> Hamilton, Ontario
> Canada L8S 4M4
> Web: socserv.mcmaster.ca/jfox
> 
> 
> 
> 
> > -Original Message-
> > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of
> Stefano Sofia
> > Sent: December 10, 2015 5:08 AM
> > To: r-help@r-project.org
> > Subject: [R] matrix which results singular but at the same time
> positive definite
> >
> > Dear list users,
> > through the "matrixcalc" package I am performing some checks of
> variance
> > matrices (which must be positive definite).
> > In this example, it happens that the matrix A here reported is
> singular but
> > positive definite. Is it possible?
> >
> >   [,1]  [,2]  [,3]  [,4]
> > [1,]  1.904255e-12 -1.904255e-12 -8.238960e-13 -1.240294e-12 [2,] -
> > 1.904255e-12  3.637979e-12  1.364242e-12  1.818989e-12 [3,] -
> 8.238960e-13
> > 1.364242e-12  4.809988e+00  7.742369e-01 [4,] -1.240294e-12
> 1.818989e-12
> > 7.742369e-01  1.090411e+00
> >
> > print(is.non.singular.matrix(A, tol = 1e-18)) FALSE
> print(is.positive.definite(A,
> > tol=1e-18)) TRUE
> >
> > Is there something wrong with this matrix?
> > Any comment will be appreciated.
> > Stefano
> >
> >
> > 
> >
> > AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere
> > informazioni confidenziali, pertanto è destinato solo a persone
> autorizzate alla
> > ricezione. I messaggi di posta elettronica per i client di Regione
> Marche
> > possono contenere informazioni confidenziali e con privilegi legali.
> Se non si è il
> > destinatario specificato, non leggere, copiare, inoltrare o archiviare
> questo
> > messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al
> mittente
> > ed eliminarlo completamente dal sistema del proprio computer. Ai sensi
> > dell'art. 6 della DGR n. 1394/2008 si segnala che, in caso di
> necessità ed
> > urgenza, la risposta al presente messaggio di posta elettronica può
> essere
> > visionata da persone estranee al destinatario.
> > IMPORTANT NOTICE: This e-mail message is intended to be received only
> by
> > persons entitled to receive the confidential information it may
> contain. E-mail
> > messages to clients of Regione Marche may contain information that is
> > confidential and legally privileged. Please do not read, copy,
> forward, or store
> > this message unless you are an intended recipient of it. If you have
> received
> > this message in error, please forward it to the sender and delete it
> completely
> > from your computer system.
> >
> >   [[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] matrix which results singular but at the same time positive definite

2015-12-10 Thread peter dalgaard
Looks like the ill-conditioning is almost entirely due to scaling, e.g.

> eigen(cov2cor(m))
$values
[1] 1.7234899 1.3380701 0.6619299 0.2765101
...

This is an annoyance in several parts of numerical linear algebra: Routines 
assume that R^n has all coordinates on a similar scale and therefore think that 
anything on the order of 1e-7 or so is effectively zero. 

Condition numbers do this too:

> kappa(m)
[1] 1.066582e+13
> kappa(cov2cor(m))
[1] 5.489243


-pd

On 10 Dec 2015, at 16:41 , Stefano Sofia  
wrote:

> Dear John,
> thank you for your considerations.
> This matrix (which is a variance matrix) is part of an algorithm for 
> forward-filtering and backward-sampling of Dynamic Linear Models (West and 
> Harrison, 1997), applied to DLM representation of ARIMA processes (Petris, 
> Petrone, Campagnoli).  It is therefore very difficult to explain why this 
> variance matrix becomes so ill conditioned. This already happens at the first 
> iteration of the algorithm. I will try to work on initial conditions and some 
> fixed parameters.
> 
> Thank you again
> Stefano
> 
> 
> 
> Da: Fox, John [j...@mcmaster.ca]
> Inviato: giovedì 10 dicembre 2015 14.41
> A: Stefano Sofia; r-help@r-project.org
> Oggetto: RE: matrix which results singular but at the same time positive  
>   definite
> 
> Dear Stefano,
> 
> You've already had a couple of informative responses directly addressing your 
> question, but are you aware how ill-conditioned the matrix is (one of the 
> responses alluded to this)?
> 
>> kappa(X, exact=TRUE)
> [1] 7.313338e+12
> 
>> eigen(X)$values
> [1] 4.964711e+00 9.356881e-01 4.863392e-12 6.788344e-13
> 
> Two of the variables have variances around 10^0 and the other two around 
> 10^-12. Of course, you haven't said anything about the context, but does it 
> really make sense to analyze the data on these scales?
> 
> Best,
> John
> 
> -
> John Fox, Professor
> McMaster University
> Hamilton, Ontario
> Canada L8S 4M4
> Web: socserv.mcmaster.ca/jfox
> 
> 
> 
> 
>> -Original Message-
>> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Stefano Sofia
>> Sent: December 10, 2015 5:08 AM
>> To: r-help@r-project.org
>> Subject: [R] matrix which results singular but at the same time positive 
>> definite
>> 
>> Dear list users,
>> through the "matrixcalc" package I am performing some checks of variance
>> matrices (which must be positive definite).
>> In this example, it happens that the matrix A here reported is singular but
>> positive definite. Is it possible?
>> 
>>  [,1]  [,2]  [,3]  [,4]
>> [1,]  1.904255e-12 -1.904255e-12 -8.238960e-13 -1.240294e-12 [2,] -
>> 1.904255e-12  3.637979e-12  1.364242e-12  1.818989e-12 [3,] -8.238960e-13
>> 1.364242e-12  4.809988e+00  7.742369e-01 [4,] -1.240294e-12  1.818989e-12
>> 7.742369e-01  1.090411e+00
>> 
>> print(is.non.singular.matrix(A, tol = 1e-18)) FALSE 
>> print(is.positive.definite(A,
>> tol=1e-18)) TRUE
>> 
>> Is there something wrong with this matrix?
>> Any comment will be appreciated.
>> Stefano
>> 
>> 
>> 
>> 
>> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere
>> informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
>> alla
>> ricezione. I messaggi di posta elettronica per i client di Regione Marche
>> possono contenere informazioni confidenziali e con privilegi legali. Se non 
>> si è il
>> destinatario specificato, non leggere, copiare, inoltrare o archiviare questo
>> messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al 
>> mittente
>> ed eliminarlo completamente dal sistema del proprio computer. Ai sensi
>> dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed
>> urgenza, la risposta al presente messaggio di posta elettronica può essere
>> visionata da persone estranee al destinatario.
>> IMPORTANT NOTICE: This e-mail message is intended to be received only by
>> persons entitled to receive the confidential information it may contain. 
>> E-mail
>> messages to clients of Regione Marche may contain information that is
>> confidential and legally privileged. Please do not read, copy, forward, or 
>> store
>> this message unless you are an intended recipient of it. If you have received
>> this message in error, please forward it to the sender and delete it 
>> completely
>> from your computer system.
>> 
>>  [[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.
> 
> 
> 
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può 

Re: [R] matrix which results singular but at the same time positive definite

2015-12-10 Thread Fox, John
Dear Peter,

> -Original Message-
> From: peter dalgaard [mailto:pda...@gmail.com]
> Sent: Thursday, December 10, 2015 11:09 AM
> To: Stefano Sofia
> Cc: Fox, John; r-help@r-project.org
> Subject: Re: [R] matrix which results singular but at the same time
> positive definite
> 
> Looks like the ill-conditioning is almost entirely due to scaling, e.g.

Yes, that's my point. Sorry I didn't make it clearer.

Best,
 John

> 
> > eigen(cov2cor(m))
> $values
> [1] 1.7234899 1.3380701 0.6619299 0.2765101
> ...
> 
> This is an annoyance in several parts of numerical linear algebra:
> Routines assume that R^n has all coordinates on a similar scale and
> therefore think that anything on the order of 1e-7 or so is effectively
> zero.
> 
> Condition numbers do this too:
> 
> > kappa(m)
> [1] 1.066582e+13
> > kappa(cov2cor(m))
> [1] 5.489243
> 
> 
> -pd
> 
> On 10 Dec 2015, at 16:41 , Stefano Sofia
> <stefano.so...@regione.marche.it> wrote:
> 
> > Dear John,
> > thank you for your considerations.
> > This matrix (which is a variance matrix) is part of an algorithm for
> forward-filtering and backward-sampling of Dynamic Linear Models (West
> and Harrison, 1997), applied to DLM representation of ARIMA processes
> (Petris, Petrone, Campagnoli).  It is therefore very difficult to
> explain why this variance matrix becomes so ill conditioned. This
> already happens at the first iteration of the algorithm. I will try to
> work on initial conditions and some fixed parameters.
> >
> > Thank you again
> > Stefano
> >
> >
> > 
> > Da: Fox, John [j...@mcmaster.ca]
> > Inviato: giovedì 10 dicembre 2015 14.41
> > A: Stefano Sofia; r-help@r-project.org
> > Oggetto: RE: matrix which results singular but at the same time
> positivedefinite
> >
> > Dear Stefano,
> >
> > You've already had a couple of informative responses directly
> addressing your question, but are you aware how ill-conditioned the
> matrix is (one of the responses alluded to this)?
> >
> >> kappa(X, exact=TRUE)
> > [1] 7.313338e+12
> >
> >> eigen(X)$values
> > [1] 4.964711e+00 9.356881e-01 4.863392e-12 6.788344e-13
> >
> > Two of the variables have variances around 10^0 and the other two
> around 10^-12. Of course, you haven't said anything about the context,
> but does it really make sense to analyze the data on these scales?
> >
> > Best,
> > John
> >
> > -
> > John Fox, Professor
> > McMaster University
> > Hamilton, Ontario
> > Canada L8S 4M4
> > Web: socserv.mcmaster.ca/jfox
> >
> >
> >
> >
> >> -Original Message-
> >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of
> Stefano Sofia
> >> Sent: December 10, 2015 5:08 AM
> >> To: r-help@r-project.org
> >> Subject: [R] matrix which results singular but at the same time
> positive definite
> >>
> >> Dear list users,
> >> through the "matrixcalc" package I am performing some checks of
> variance
> >> matrices (which must be positive definite).
> >> In this example, it happens that the matrix A here reported is
> singular but
> >> positive definite. Is it possible?
> >>
> >>  [,1]  [,2]  [,3]  [,4]
> >> [1,]  1.904255e-12 -1.904255e-12 -8.238960e-13 -1.240294e-12 [2,] -
> >> 1.904255e-12  3.637979e-12  1.364242e-12  1.818989e-12 [3,] -
> 8.238960e-13
> >> 1.364242e-12  4.809988e+00  7.742369e-01 [4,] -1.240294e-12
> 1.818989e-12
> >> 7.742369e-01  1.090411e+00
> >>
> >> print(is.non.singular.matrix(A, tol = 1e-18)) FALSE
> print(is.positive.definite(A,
> >> tol=1e-18)) TRUE
> >>
> >> Is there something wrong with this matrix?
> >> Any comment will be appreciated.
> >> Stefano
> >>
> >>
> >> 
> >>
> >> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può
> contenere
> >> informazioni confidenziali, pertanto è destinato solo a persone
> autorizzate alla
> >> ricezione. I messaggi di posta elettronica per i client di Regione
> Marche
> >> possono contenere informazioni confidenziali e con privilegi legali.
> Se non si è il
> >> destinatario specificato, non leggere, copiare, inoltrare o
> archiviare questo
> >> messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo
> al mittente
> >

Re: [R] Matrix element-by-element multiplication

2015-09-29 Thread Rolf Turner

On 30/09/15 12:49, waddawanna wrote:

Hello Steven,

It looks like, there is no in-built function that can do GAUSS ".*"
element-wise multiplication.
Now, if you want to make the desired computations in R, it is actually
preatty straightforward.


a<-c(1,2,3)
b<-matrix(rep(1:9,1),3,3,byrow=TRUE)
a*b


That, should work fine. But, suppose that for some reason you have following
situation, which can make you trip for hours of sleepless nights. That is,
you have a matrix "b", where number of columns equal to number of columns of
your vector "a". That is


b<-matrix(rep(1:9,1),3,3,byrow=TRUE);b

  [,1] [,2] [,3]
[1,]123
[2,]456
[3,]789


a <- matrix(rep(1:3,1),1,3,byrow=TRUE)

  [,1] [,2] [,3]
[1,]123

If you try to do elementwise multilication, i.e., of those two

b*a


You get an error that they are not comfomable, that is why, you have to
write your own function (here,
I just write the for-loop):

for ( i in 1:3 ) {

  foo[ ,i] = ( foo[ ,i] * bar[1,i] ) ;
}
[,1] [,2] [,3]
[1,]149
[2,]4   10   18
[3,]7   16   27

I hope that this helped.



(1) You're making heavy weather by using rep() totally unnecessarily.


(2) The example you give can be done much more succinctly; for-loops
are unnecessary:

   t(as.vector(a)*t(b))

(3) Please don't post to R-help via nabble (WTF ever that is).  It 
messes up everything.


cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
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] Matrix element-by-element multiplication

2015-09-29 Thread David Winsemius

On Sep 29, 2015, at 4:49 PM, waddawanna wrote:

> Hello Steven,
> 
> It looks like, there is no in-built function that can do GAUSS ".*"
> element-wise multiplication.
> Now, if you want to make the desired computations in R, it is actually
> preatty straightforward.
> 
>> a<-c(1,2,3)
>> b<-matrix(rep(1:9,1),3,3,byrow=TRUE)
>> a*b
> 
> That, should work fine. But, suppose that for some reason you have following
> situation, which can make you trip for hours of sleepless nights. That is,
> you have a matrix "b", where number of columns equal to number of columns of
> your vector "a". That is
> 
>> b<-matrix(rep(1:9,1),3,3,byrow=TRUE);b
> [,1] [,2] [,3]
> [1,]123
> [2,]456
> [3,]789
> 
>> a <- matrix(rep(1:3,1),1,3,byrow=TRUE)
> [,1] [,2] [,3]
> [1,]123
> 
> If you try to do elementwise multilication, i.e., of those two
>> b*a
> 
> You get an error that they are not comfomable,

You should not have gotten that error with either `*` or `%*%`.

> that is why, you have to
> write your own function (here, 
> I just write the for-loop):
>> for ( i in 1:3 ) {
> foo[ ,i] = ( foo[ ,i] * bar[1,i] ) ;
>   }
>   [,1] [,2] [,3]
> [1,]149
> [2,]4   10   18
> [3,]7   16   27

You were expecting c(1,2,3) to be a row-vector. That leads to disappointment. 
If anything it will be a column-vector.

Notice  no error:
> a*b
 [,1] [,2] [,3]
[1,]123
[2,]8   10   12
[3,]   21   24   27


You really wanted to sweep that vector

> sweep(b, 1,a,'*')   # Sweeping by rows is same a multiplying by recycle 
> column vector 
 [,1] [,2] [,3]
[1,]123
[2,]8   10   12
[3,]   21   24   27

> sweep(b, 2, a, '*')  # You wanted to "sweep" across columns
 [,1] [,2] [,3]
[1,]149
[2,]4   10   18
[3,]7   16   27

-- 

David Winsemius
Alameda, CA, USA

__
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] matrix manipulation

2015-07-16 Thread Peter Langfelder
Hi Terry,

maybe I'm missing something, but why not define a matrix BB = V'B;
then t(B) %*% V = t(BB), then your problem reduces to finding A such
that t(BB) %*% A = 0?

Peter

On Thu, Jul 16, 2015 at 10:28 AM, Therneau, Terry M., Ph.D.
thern...@mayo.edu wrote:
 This is as much a mathematics as an R question, in the this should be easy
 but I don't see it category.

 Assume I have a full rank p by p matrix V  (aside: V = (X'X)^{-1} for a
 particular setup), a p by k matrix B, and I want to complete an orthagonal
 basis for the space with distance function V.  That is, find A such that
 t(B) %*% V %*% A =0, where A has p rows and p-k columns.

 With V=identity this is easy. I can do it in 1-2 lines using qr(), lm(), or
 several other tools.  A part of me is quite certain that the general problem
 isn't more than 3 lines of R, but after a day of beating my head on the
 issue I still don't see it.  Math wise it looks like a simple homework
 problem in a mid level class, but I'm not currently sure that I'd pass said
 class.

 If someone could show the way I would be grateful.  Either that or assurance
 that the problem actually IS hard and I'm not as dense as I think.

 Terry T.

 __
 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] matrix manipulation -solved

2015-07-16 Thread Therneau, Terry M., Ph.D.

Yes it is obvious --- once someone else pointed it out.
Thanks for the hint.

Terry T.


On 07/16/2015 12:52 PM, Peter Langfelder wrote:

Hi Terry,

maybe I'm missing something, but why not define a matrix BB = V'B;
then t(B) %*% V = t(BB), then your problem reduces to finding A such
that t(BB) %*% A = 0?

Peter

On Thu, Jul 16, 2015 at 10:28 AM, Therneau, Terry M., Ph.D.
thern...@mayo.edu wrote:

This is as much a mathematics as an R question, in the this should be easy
but I don't see it category.

Assume I have a full rank p by p matrix V  (aside: V = (X'X)^{-1} for a
particular setup), a p by k matrix B, and I want to complete an orthagonal
basis for the space with distance function V.  That is, find A such that
t(B) %*% V %*% A =0, where A has p rows and p-k columns.

With V=identity this is easy. I can do it in 1-2 lines using qr(), lm(), or
several other tools.  A part of me is quite certain that the general problem
isn't more than 3 lines of R, but after a day of beating my head on the
issue I still don't see it.  Math wise it looks like a simple homework
problem in a mid level class, but I'm not currently sure that I'd pass said
class.

If someone could show the way I would be grateful.  Either that or assurance
that the problem actually IS hard and I'm not as dense as I think.

Terry T.

__
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] Matrix Manipulation R

2015-07-04 Thread David Winsemius

 On Jul 4, 2015, at 3:09 AM, Alex Kim dumboisveryd...@gmail.com wrote:
 
 Hi guys,
 
 Suppose I have an extremely large data frame with 2 columns and .5 mil
 rows. For example, the last 6 rows may look like this:
 .
 ..
 ...
 89 100
 93 120
 95 125
 101NA
 115NA
 123NA
 124NA
 
 I would like to manipulate this data frame to output a data frame that
 looks like:,
 
 10089, 93, 95
 120101, 115
 125123, 124
 

 What would be the absolute quickest way to do this, given that there are
 many rows? Currently I have this:
 
 # m is the large two column data frame
 end - na.omit(m[,'V2']);
 out - data.frame(End=end,
 Start=unname(sapply(split(m[,'V1'],findInterval(m[,'V1'],end))[as.character(0:c(length(end)-1))],paste,collapse='.')))
 

This might be a little faster. It skips some of the steps in your version:

 dput(m)
structure(list(V1 = c(89, 93, 95, 101, 115, 123, 124), V2 = c(100, 
120, 125, NA, NA, NA, NA)), .Names = c(V1, V2), row.names = c(NA, 
-7L), class = data.frame)

end - na.omit(m[,'V2’])
# this will only work if that vector is sorted
data.frame(End = end,
   Start = sapply( split( m$V1, 
 findInterval(m$V1, c(-Inf, end))), 
  paste,collapse=, ) )
  EndStart
1 100 89,93,95
2 120  101,115
3 125  123,124


 However this is taking a little bit too long.
 
 Thank you for your help!
 
   [[alternative HTML version deleted]]

This is a plain-text mailing list and posting triplicate questions is poor form.

Do read the posting guide.

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

— 
David Winsemius, MD
Alameda, CA, USA

__
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] matrix - delete last row - list

2015-07-04 Thread Michael Sumner
Well 'list' in R is pretty naturally the same as R's 'atomic vector' in
scads of languages. In R the term needs special care since it's still a
'vector'.

'Degenerate dimension' is probably a helpful phrase for understanding what
is happening here.

Cheers, Mike

On Saturday, July 4, 2015, Rolf Turner r.tur...@auckland.ac.nz wrote:
 On 04/07/15 03:43, Sarah Goslee wrote:

 Hi,

 On Fri, Jul 3, 2015 at 10:33 AM, Zander, Joscha joscha.zan...@roche.com
 wrote:

 Good day R-community,

 i just wondered if it is a bug or a feature...

 When i have a matrix mat with one column and i delete the last row
with

 mat - mat[-nrow(mat),] the result is a list.

 I have no idea how you're getting a list from a matrix (see below).
Perhaps
 you mean a data frame?

 SNIP

 No.  The Zander person just means a vector.  Psigh.

 Note to the Zander person:  It is important in R programming to get your
concepts straight.  It is also important in communicating with others to
get your terminology straight.

 cheers,

 Rolf Turner

 --
 Technical Editor ANZJS
 Department of Statistics
 University of Auckland
 Phone: +64-9-373-7599 ext. 88276

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


-- 
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsum...@gmail.com

[[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] matrix - delete last row - list

2015-07-03 Thread Sarah Goslee
Hi,

On Fri, Jul 3, 2015 at 10:33 AM, Zander, Joscha joscha.zan...@roche.com
wrote:
 Good day R-community,

 i just wondered if it is a bug or a feature...

 When i have a matrix mat with one column and i delete the last row with

 mat - mat[-nrow(mat),] the result is a list.

I have no idea how you're getting a list from a matrix (see below). Perhaps
you mean a data frame?


 So my next call mat[10,] will throw an wrong dimension error.
 The proper call must be:

 mat - as.matrix(mat[-nrow(mat),])

 So is this desired behavior or a bug?

If you check
?[
you'll see the drop argument, which is what I guess you want. Compare:

 mat - matrix(1:6, nrow=2)
 mat - mat[-nrow(mat), ]
 class(mat) # not a list
[1] integer
 dim(mat)
NULL
 is.list(mat) # see? really not a list
[1] FALSE
 mat
[1] 1 3 5


 mat - matrix(1:6, nrow=2)
 mat - mat[-nrow(mat), , drop=FALSE]
 class(mat)
[1] matrix
 dim(mat)
[1] 1 3
 mat
 [,1] [,2] [,3]
[1,]135



 I use R-version 2.15.3, but reconstructed this behavior in 3.2.0 as well.

 greetings

 --
  *Joscha Zander*
--
Sarah Goslee
http://www.functionaldiversity.org

[[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] matrix - delete last row - list

2015-07-03 Thread Marc Schwartz

 On Jul 3, 2015, at 9:33 AM, Zander, Joscha joscha.zan...@roche.com wrote:
 
 Good day R-community,
 
 i just wondered if it is a bug or a feature...
 
 When i have a matrix mat with one column and i delete the last row with
 
 mat - mat[-nrow(mat),] the result is a list.
 
 So my next call mat[10,] will throw an wrong dimension error.
 The proper call must be:
 
 mat - as.matrix(mat[-nrow(mat),])
 
 So is this desired behavior or a bug?
 
 I use R-version 2.15.3, but reconstructed this behavior in 3.2.0 as well.
 
 greetings
 
 -- 
 *Joscha Zander*


See:

  
http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-dimensions_003f


mat - matrix(1:12, ncol = 1)

 str(mat)
 int [1:12, 1] 1 2 3 4 5 6 7 8 9 10 ...

 mat[-nrow(mat), ]
 [1]  1  2  3  4  5  6  7  8  9 10 11

# This is a vector, not a list
 str(mat[-nrow(mat), ])
 int [1:11] 1 2 3 4 5 6 7 8 9 10 …

 mat[-nrow(mat), , drop = FALSE]
  [,1]
 [1,]1
 [2,]2
 [3,]3
 [4,]4
 [5,]5
 [6,]6
 [7,]7
 [8,]8
 [9,]9
[10,]   10
[11,]   11

# This is a matrix
 str(mat[-nrow(mat), , drop = FALSE])
 int [1:11, 1] 1 2 3 4 5 6 7 8 9 10 …


Regards,

Marc Schwartz

P.S. are you restricted in being able to upgrade from a version of R that is 
two years old?

__
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] matrix/df help populate NA

2015-06-14 Thread jim holtman
Is this what you want:

 x1 = structure(list(Subject = c(x1, x2), A = c(1.5, -1.2), B = c(-1.3,
+ -0.3), C = c(0.4, 0.3), D = c(-0.2, -0.1)), .Names = c(Subject,
+ A, B, C, D), class = data.frame, row.names = c(NA,
+ -2L))

 x2 = structure(list(Subject = c(x1, x2), A = c(4.3, 2.4), D = c(-2.4,
+ 0.1), F = c(1.3, 0.5), H = c(-2.3, -1.4)), .Names = c(Subject,
+ A, D, F, H), class = data.frame, row.names = c(NA,
+ -2L))

 # determine what the missing columns are and then add them to x2
 missing - setdiff(colnames(x1), colnames(x2))

 new_x2 - x2

 for (i in missing) new_x2[[i]] - NA

 new_x2
  Subject   AD   FH  B  C
1  x1 4.3 -2.4 1.3 -2.3 NA NA
2  x2 2.4  0.1 0.5 -1.4 NA NA





Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Sat, Jun 13, 2015 at 11:17 PM, Adrian Johnson oriolebaltim...@gmail.com
wrote:

 Dear group:

 I have two data frames. The column names of the two data frame has
 some common variables but not identical.

 my aim is to make 2  DFs more uniform by taking union of both colnames


 For example: I have x1 and x2 matrices:

  x1
   SubjectAB   CD
 1  x1  1.5 -1.3 0.4 -0.2
 2  x2 -1.2 -0.3 0.3 -0.1
  x2
   Subject   AD   FH
 1  x1 4.3 -2.4 1.3 -2.3
 2  x2 2.4  0.1 0.5 -1.4

  cases = c('A','B','C','D','F','H')

 for X2 I want to create newX2 DF.

  x3
   Subject   A  B  CD   FH
 1  x1 4.3 NA NA -2.4 1.3 -2.3
 2  x2 2.4 NA NA  0.1 0.5 -1.4


 Since B and C are no existing in x2, I put NAs.

 how can I create x3 matrix?



 dput code:

 x1 = structure(list(Subject = c(x1, x2), A = c(1.5, -1.2), B = c(-1.3,
 -0.3), C = c(0.4, 0.3), D = c(-0.2, -0.1)), .Names = c(Subject,
 A, B, C, D), class = data.frame, row.names = c(NA,
 -2L))

 x2 = structure(list(Subject = c(x1, x2), A = c(4.3, 2.4), D = c(-2.4,
 0.1), F = c(1.3, 0.5), H = c(-2.3, -1.4)), .Names = c(Subject,
 A, D, F, H), class = data.frame, row.names = c(NA,
 -2L))


 Could you please help how to create x3 with NAs incorporated.
 adrian.

 __
 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] matrix/df help populate NA

2015-06-14 Thread Adrian Johnson
Thank you very much. It worked!


On Sun, Jun 14, 2015 at 8:00 PM, jim holtman jholt...@gmail.com wrote:
 Is this what you want:

 x1 = structure(list(Subject = c(x1, x2), A = c(1.5, -1.2), B = c(-1.3,
 + -0.3), C = c(0.4, 0.3), D = c(-0.2, -0.1)), .Names = c(Subject,
 + A, B, C, D), class = data.frame, row.names = c(NA,
 + -2L))

 x2 = structure(list(Subject = c(x1, x2), A = c(4.3, 2.4), D = c(-2.4,
 + 0.1), F = c(1.3, 0.5), H = c(-2.3, -1.4)), .Names = c(Subject,
 + A, D, F, H), class = data.frame, row.names = c(NA,
 + -2L))

 # determine what the missing columns are and then add them to x2
 missing - setdiff(colnames(x1), colnames(x2))

 new_x2 - x2

 for (i in missing) new_x2[[i]] - NA

 new_x2
   Subject   AD   FH  B  C
 1  x1 4.3 -2.4 1.3 -2.3 NA NA
 2  x2 2.4  0.1 0.5 -1.4 NA NA





 Jim Holtman
 Data Munger Guru

 What is the problem that you are trying to solve?
 Tell me what you want to do, not how you want to do it.

 On Sat, Jun 13, 2015 at 11:17 PM, Adrian Johnson oriolebaltim...@gmail.com
 wrote:

 Dear group:

 I have two data frames. The column names of the two data frame has
 some common variables but not identical.

 my aim is to make 2  DFs more uniform by taking union of both colnames


 For example: I have x1 and x2 matrices:

  x1
   SubjectAB   CD
 1  x1  1.5 -1.3 0.4 -0.2
 2  x2 -1.2 -0.3 0.3 -0.1
  x2
   Subject   AD   FH
 1  x1 4.3 -2.4 1.3 -2.3
 2  x2 2.4  0.1 0.5 -1.4

  cases = c('A','B','C','D','F','H')

 for X2 I want to create newX2 DF.

  x3
   Subject   A  B  CD   FH
 1  x1 4.3 NA NA -2.4 1.3 -2.3
 2  x2 2.4 NA NA  0.1 0.5 -1.4


 Since B and C are no existing in x2, I put NAs.

 how can I create x3 matrix?



 dput code:

 x1 = structure(list(Subject = c(x1, x2), A = c(1.5, -1.2), B = c(-1.3,
 -0.3), C = c(0.4, 0.3), D = c(-0.2, -0.1)), .Names = c(Subject,
 A, B, C, D), class = data.frame, row.names = c(NA,
 -2L))

 x2 = structure(list(Subject = c(x1, x2), A = c(4.3, 2.4), D = c(-2.4,
 0.1), F = c(1.3, 0.5), H = c(-2.3, -1.4)), .Names = c(Subject,
 A, D, F, H), class = data.frame, row.names = c(NA,
 -2L))


 Could you please help how to create x3 with NAs incorporated.
 adrian.

 __
 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] matrix/df help populate NA

2015-06-13 Thread Jeff Newmiller
?merge

Particularly look at the all argument.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On June 13, 2015 8:17:35 PM PDT, Adrian Johnson oriolebaltim...@gmail.com 
wrote:
Dear group:

I have two data frames. The column names of the two data frame has
some common variables but not identical.

my aim is to make 2  DFs more uniform by taking union of both colnames


For example: I have x1 and x2 matrices:

 x1
  SubjectAB   CD
1  x1  1.5 -1.3 0.4 -0.2
2  x2 -1.2 -0.3 0.3 -0.1
 x2
  Subject   AD   FH
1  x1 4.3 -2.4 1.3 -2.3
2  x2 2.4  0.1 0.5 -1.4

 cases = c('A','B','C','D','F','H')

for X2 I want to create newX2 DF.

 x3
  Subject   A  B  CD   FH
1  x1 4.3 NA NA -2.4 1.3 -2.3
2  x2 2.4 NA NA  0.1 0.5 -1.4


Since B and C are no existing in x2, I put NAs.

how can I create x3 matrix?



dput code:

x1 = structure(list(Subject = c(x1, x2), A = c(1.5, -1.2), B =
c(-1.3,
-0.3), C = c(0.4, 0.3), D = c(-0.2, -0.1)), .Names = c(Subject,
A, B, C, D), class = data.frame, row.names = c(NA,
-2L))

x2 = structure(list(Subject = c(x1, x2), A = c(4.3, 2.4), D =
c(-2.4,
0.1), F = c(1.3, 0.5), H = c(-2.3, -1.4)), .Names = c(Subject,
A, D, F, H), class = data.frame, row.names = c(NA,
-2L))


Could you please help how to create x3 with NAs incorporated.
adrian.

__
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] Matrix of indexes to extract sparse data in dataframe

2015-06-05 Thread Sergio Fonda
Thank you, of course but I can't use that form as I told. My question is
about the possibility to enter in a dataframe with a matrix of indices and
get the corresponding values
Thanks again
 Il 05/giu/2015 15:39, John Kane jrkrid...@inbox.com ha scritto:

 d1  -  apply(c0, 1, min)  I think does it.

 John Kane
 Kingston ON Canada


  -Original Message-
  From: sergio.fond...@gmail.com
  Sent: Fri, 5 Jun 2015 15:06:34 +0200
  To: r-help@r-project.org
  Subject: [R] Matrix of indexes to extract sparse data in dataframe
 
  I would like to avoid a for loop to get a vector of data taken from
  rows of a data frame for specific columns.
  An example is the following (I can't apply min to every row of df, this
  is
  just an example):
 
  c0=data.frame(a=c(3,-2,12,7,-23,17) , b=c(-1,-3,14,2,6,19))
  c1=apply(c0,1,which.min)
  c1
  [1] 2 2 1 2 1 1
 
  I would like to get a result like the following call, but without
  employing a for loop:
 
  d1=c(c0[1,c1[1]], c0[2,c1[2]], c0[3,c1[3]], c0[4,c1[4]], c0[5,c1[5]],
  c0[6,c1[6]])
  d1
  [1]  -1  -3  12   2 -23  17
 
  Thanks a lot for any help!
 
  __
  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.

 
 Can't remember your password? Do you need a strong and secure password?
 Use Password manager! It stores your passwords  protects your account.
 Check it out at http://mysecurelogon.com/password-manager




[[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] Matrix of indexes to extract sparse data in dataframe

2015-06-05 Thread John Kane
d1  -  apply(c0, 1, min)  I think does it.

John Kane
Kingston ON Canada


 -Original Message-
 From: sergio.fond...@gmail.com
 Sent: Fri, 5 Jun 2015 15:06:34 +0200
 To: r-help@r-project.org
 Subject: [R] Matrix of indexes to extract sparse data in dataframe
 
 I would like to avoid a for loop to get a vector of data taken from
 rows of a data frame for specific columns.
 An example is the following (I can't apply min to every row of df, this
 is
 just an example):
 
 c0=data.frame(a=c(3,-2,12,7,-23,17) , b=c(-1,-3,14,2,6,19))
 c1=apply(c0,1,which.min)
 c1
 [1] 2 2 1 2 1 1
 
 I would like to get a result like the following call, but without
 employing a for loop:
 
 d1=c(c0[1,c1[1]], c0[2,c1[2]], c0[3,c1[3]], c0[4,c1[4]], c0[5,c1[5]],
 c0[6,c1[6]])
 d1
 [1]  -1  -3  12   2 -23  17
 
 Thanks a lot for any help!
 
 __
 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.


Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords  protects your account.

__
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] Matrix of indexes to extract sparse data in dataframe

2015-06-05 Thread David L Carlson
You can select elements of a matrix using a 2 dimensional matrix that specifies 
the row/column number of the cells you want to extract:

 c2 - cbind(seq_len(nrow(c0)), c1)
 c2
   c1
[1,] 1  2
[2,] 2  2
[3,] 3  1
[4,] 4  2
[5,] 5  1
[6,] 6  1
 d1 - c0[c2]
 d1
[1]  -1  -3  12   2 -23  17

See the help page for [

?'['

-
David L Carlson
Department of Anthropology
Texas AM University
College Station, TX 77840-4352


-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Sergio Fonda
Sent: Friday, June 5, 2015 8:47 AM
To: John Kane
Cc: R-help
Subject: Re: [R] Matrix of indexes to extract sparse data in dataframe

Thank you, of course but I can't use that form as I told. My question is
about the possibility to enter in a dataframe with a matrix of indices and
get the corresponding values
Thanks again
 Il 05/giu/2015 15:39, John Kane jrkrid...@inbox.com ha scritto:

 d1  -  apply(c0, 1, min)  I think does it.

 John Kane
 Kingston ON Canada


  -Original Message-
  From: sergio.fond...@gmail.com
  Sent: Fri, 5 Jun 2015 15:06:34 +0200
  To: r-help@r-project.org
  Subject: [R] Matrix of indexes to extract sparse data in dataframe
 
  I would like to avoid a for loop to get a vector of data taken from
  rows of a data frame for specific columns.
  An example is the following (I can't apply min to every row of df, this
  is
  just an example):
 
  c0=data.frame(a=c(3,-2,12,7,-23,17) , b=c(-1,-3,14,2,6,19))
  c1=apply(c0,1,which.min)
  c1
  [1] 2 2 1 2 1 1
 
  I would like to get a result like the following call, but without
  employing a for loop:
 
  d1=c(c0[1,c1[1]], c0[2,c1[2]], c0[3,c1[3]], c0[4,c1[4]], c0[5,c1[5]],
  c0[6,c1[6]])
  d1
  [1]  -1  -3  12   2 -23  17
 
  Thanks a lot for any help!
 
  __
  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.

 
 Can't remember your password? Do you need a strong and secure password?
 Use Password manager! It stores your passwords  protects your account.
 Check it out at http://mysecurelogon.com/password-manager




[[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] Matrix of indexes to extract sparse data in dataframe

2015-06-05 Thread Sergio Fonda
Thank you very much!
Il 05/giu/2015 15:58, David L Carlson dcarl...@tamu.edu ha scritto:

 You can select elements of a matrix using a 2 dimensional matrix that
 specifies the row/column number of the cells you want to extract:

  c2 - cbind(seq_len(nrow(c0)), c1)
  c2
c1
 [1,] 1  2
 [2,] 2  2
 [3,] 3  1
 [4,] 4  2
 [5,] 5  1
 [6,] 6  1
  d1 - c0[c2]
  d1
 [1]  -1  -3  12   2 -23  17

 See the help page for [

 ?'['

 -
 David L Carlson
 Department of Anthropology
 Texas AM University
 College Station, TX 77840-4352


 -Original Message-
 From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Sergio
 Fonda
 Sent: Friday, June 5, 2015 8:47 AM
 To: John Kane
 Cc: R-help
 Subject: Re: [R] Matrix of indexes to extract sparse data in dataframe

 Thank you, of course but I can't use that form as I told. My question is
 about the possibility to enter in a dataframe with a matrix of indices and
 get the corresponding values
 Thanks again
  Il 05/giu/2015 15:39, John Kane jrkrid...@inbox.com ha scritto:

  d1  -  apply(c0, 1, min)  I think does it.
 
  John Kane
  Kingston ON Canada
 
 
   -Original Message-
   From: sergio.fond...@gmail.com
   Sent: Fri, 5 Jun 2015 15:06:34 +0200
   To: r-help@r-project.org
   Subject: [R] Matrix of indexes to extract sparse data in dataframe
  
   I would like to avoid a for loop to get a vector of data taken from
   rows of a data frame for specific columns.
   An example is the following (I can't apply min to every row of df, this
   is
   just an example):
  
   c0=data.frame(a=c(3,-2,12,7,-23,17) , b=c(-1,-3,14,2,6,19))
   c1=apply(c0,1,which.min)
   c1
   [1] 2 2 1 2 1 1
  
   I would like to get a result like the following call, but without
   employing a for loop:
  
   d1=c(c0[1,c1[1]], c0[2,c1[2]], c0[3,c1[3]], c0[4,c1[4]], c0[5,c1[5]],
   c0[6,c1[6]])
   d1
   [1]  -1  -3  12   2 -23  17
  
   Thanks a lot for any help!
  
   __
   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.
 
  
  Can't remember your password? Do you need a strong and secure password?
  Use Password manager! It stores your passwords  protects your account.
  Check it out at http://mysecurelogon.com/password-manager
 
 
 

 [[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] matrix inf and zero's value replacement

2015-05-13 Thread Uwe Ligges



On 13.05.2015 08:04, Ragia Ibrahim wrote:

Dear Group,
I have the following matrix
m
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]021  Inf  Inf  Inf  Inf  Inf
[2,]102  Inf  Inf  Inf  Inf  Inf
[3,]210  Inf  Inf  Inf  Inf  Inf
[4,]3210  Inf  Inf  Inf  Inf
[5,]  Inf  Inf  Inf  Inf0  Inf  Inf  Inf
[6,]  Inf  Inf  Inf  Inf10  Inf  Inf
[7,]  Inf  Inf  Inf  Inf  Inf  Inf0  Inf
[8,]132  Inf  Inf  Inf10

I want  all values grater than 0 = to 1 and zero other wise?
thanks in advance
so
this used,
m -ifelse(  (m==0)||  is.infinite(m),0, 1   )

but it gave me  zero   result
replacing
|| with | ,make sense and return the matrix I was looking for.

what is the difference betwen boht || an | ? when to use each ?


|| only for scalars, see the documentation.

From your text you want something different:
m[m0] - 1

but your code says you want to replace Inf by 0?

Best,
Uwe Ligges




thanks in advance

[[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] matrix manipulation question

2015-03-31 Thread Stéphane Adamowicz
Many thanks,

Stéphane

Le 30 mars 2015 à 10:42, peter dalgaard pda...@gmail.com a écrit :

 
 On 30 Mar 2015, at 09:59 , Stéphane Adamowicz 
 stephane.adamow...@avignon.inra.fr wrote:
 
 
 However, in order to help me understand, would you be so kind as to give me 
 a matrix or data.frame example where « complete.cases(X)== T » or « 
 complete.cases(X)== TRUE » would give some unwanted result ?
 
 The standard problem with T for TRUE is if T has been used for some other 
 purpose, like a time variable. E.g., T - 0 ; complete.cases(X)==T.
 
 complete.cases()==TRUE is just silly, like (x==0)==TRUE or 
 ((x==0)==TRUE)==TRUE). 
 
 (However, notice that x==TRUE is different from as.logical(x) if x is 
 numeric, so ifelse(x,y,z) may differ from ifelse(x==TRUE,y,z).) 
 
 -- 
 Peter Dalgaard, Professor,
 Center for Statistics, Copenhagen Business School
 Solbjerg Plads 3, 2000 Frederiksberg, Denmark
 Phone: (+45)38153501
 Email: pd@cbs.dk  Priv: pda...@gmail.com
 
 
 
 
 
 
 
 

__
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] matrix manipulation question

2015-03-30 Thread Berend Hasselman

 On 30-03-2015, at 09:59, Stéphane Adamowicz 
 stephane.adamow...@avignon.inra.fr wrote:
 
 
 Le 27 mars 2015 à 18:01, David Winsemius dwinsem...@comcast.net a écrit :
 
 
 On Mar 27, 2015, at 3:41 AM, Stéphane Adamowicz wrote:
 
 Well, it seems to work with me.
 
 
 No one is doubting that it worked for you in this instance. What Peter D. 
 was criticizing was the construction :
 
 complete.cases(t(Y))==T
 
 ... and it was on two bases that it is wrong. The first is that `T` is not 
 guaranteed to be TRUE. The second is that the test ==T (or similarly ==TRUE) 
 is completely unnecessary because `complete.cases` returns a logical vector 
 and so that expression is a waste of time.
 
 
 Indeed, You are right, the following code was enough :
 «  Z - Y[, complete.cases(t(Y) ] »
 
 
 However, in order to help me understand, would you be so kind as to give me a 
 matrix or data.frame example where « complete.cases(X)== T » or « 
 complete.cases(X)== TRUE » would give some unwanted result ?

T can be redefined.
Try this in your example with airquality:

T - hello
Z - Y[,complete.cases(t(Y))==T]
Z

TRUE is a reserved word and cannot be changed. But why use ==TRUE if not 
necessary?
All of this mentioned already by David Winsemius in a previous reply.

Berend

__
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] matrix manipulation question

2015-03-30 Thread peter dalgaard

 On 30 Mar 2015, at 09:59 , Stéphane Adamowicz 
 stephane.adamow...@avignon.inra.fr wrote:
 
 
 However, in order to help me understand, would you be so kind as to give me a 
 matrix or data.frame example where « complete.cases(X)== T » or « 
 complete.cases(X)== TRUE » would give some unwanted result ?

The standard problem with T for TRUE is if T has been used for some other 
purpose, like a time variable. E.g., T - 0 ; complete.cases(X)==T.

complete.cases()==TRUE is just silly, like (x==0)==TRUE or 
((x==0)==TRUE)==TRUE). 

(However, notice that x==TRUE is different from as.logical(x) if x is numeric, 
so ifelse(x,y,z) may differ from ifelse(x==TRUE,y,z).) 

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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] matrix manipulation question

2015-03-30 Thread Stéphane Adamowicz

Le 27 mars 2015 � 18:01, David Winsemius dwinsem...@comcast.net a �crit :

 
 On Mar 27, 2015, at 3:41 AM, St�phane Adamowicz wrote:
 
 Well, it seems to work with me.
 
 
 No one is doubting that it worked for you in this instance. What Peter D. was 
 criticizing was the construction :
 
 complete.cases(t(Y))==T
 
 ... and it was on two bases that it is wrong. The first is that `T` is not 
 guaranteed to be TRUE. The second is that the test ==T (or similarly ==TRUE) 
 is completely unnecessary because `complete.cases` returns a logical vector 
 and so that expression is a waste of time.
 

Indeed, You are right, the following code was enough :
�  Z - Y[, complete.cases(t(Y) ] �


However, in order to help me understand, would you be so kind as to give me a 
matrix or data.frame example where � complete.cases(X)== T � or � 
complete.cases(X)== TRUE � would give some unwanted result ?

St�phane


[[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] matrix manipulation question

2015-03-27 Thread peter dalgaard

On 27 Mar 2015, at 09:58 , Stéphane Adamowicz 
stephane.adamow...@avignon.inra.fr wrote:

 data_no_NA - data[, complete.cases(t(data))==T]

Ouch! logical == TRUE is bad, logical == T is worse:

data[, complete.cases(t(data))]


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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] matrix manipulation question

2015-03-27 Thread PIKAL Petr
Very, very, very bad solution.

as.matrix can change silently your data to unwanted format, complete.cases()==T 
is silly as Peter already pointed out.

I use

head(airquality[ ,colSums(is.na(airquality))==0])
  Wind Temp Month Day
1  7.4   67 5   1
2  8.0   72 5   2
3 12.6   74 5   3
4 11.5   62 5   4
5 14.3   56 5   5
6 14.9   66 5   6

if I want to get rid of columns with NA.

Cheers
Petr


From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Stéphane 
Adamowicz
Sent: Friday, March 27, 2015 11:42 AM
To: peter dalgaard
Cc: r-help@r-project.org
Subject: Re: [R] matrix manipulation question

Well, it seems to work with me.

Y - as.matrix(airquality)
head(Y, n=8)
 Ozone Solar.R Wind Temp Month Day
[1,]41 190  7.4   67 5   1
[2,]36 118  8.0   72 5   2
[3,]12 149 12.6   74 5   3
[4,]18 313 11.5   62 5   4
[5,]NA  NA 14.3   56 5   5
[6,]28  NA 14.9   66 5   6
[7,]23 299  8.6   65 5   7
[8,]19  99 13.8   59 5   8

Z - Y[,complete.cases(t(Y))==T]

head(Z, n=8)
 Wind Temp Month Day
[1,]  7.4   67 5   1
[2,]  8.0   72 5   2
[3,] 12.6   74 5   3
[4,] 11.5   62 5   4
[5,] 14.3   56 5   5
[6,] 14.9   66 5   6
[7,]  8.6   65 5   7
[8,] 13.8   59 5   8

The columns that contained NA were deleted.


Le 27 mars 2015 � 10:38, peter dalgaard 
pda...@gmail.commailto:pda...@gmail.com a �crit :


 On 27 Mar 2015, at 09:58 , St�phane Adamowicz 
 stephane.adamow...@avignon.inra.frmailto:stephane.adamow...@avignon.inra.fr
  wrote:

 data_no_NA - data[, complete.cases(t(data))==T]

 Ouch! logical == TRUE is bad, logical == T is worse:

 data[, complete.cases(t(data))]


 --
 Peter Dalgaard, Professor,
 Center for Statistics, Copenhagen Business School
 Solbjerg Plads 3, 2000 Frederiksberg, Denmark
 Phone: (+45)38153501
 Office: A 4.23
 Email: pd@cbs.dkmailto:pd@cbs.dk  Priv: 
 pda...@gmail.commailto:pda...@gmail.com












_
St�phane Adamowicz
Inra, centre de recherche Paca, unit� PSH
228, route de l'a�rodrome
CS 40509
domaine St Paul, site Agroparc
84914 Avignon, cedex 9
France

stephane.adamow...@avignon.inra.frmailto:stephane.adamow...@avignon.inra.fr
tel.  +33 (0)4 32 72 24 35
fax. +33 (0)4 32 72 24 32
do not dial 0 when out of France
web PSH  : https://www6.paca.inra.fr/psh
web Inra : http://www.inra.fr/
_


[[alternative HTML version deleted]]
__
R-help@r-project.orgmailto: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.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without

Re: [R] matrix manipulation question

2015-03-27 Thread Stéphane Adamowicz
Well, it seems to work with me.

Y - as.matrix(airquality)
head(Y, n=8)
 Ozone Solar.R Wind Temp Month Day
[1,]41 190  7.4   67 5   1
[2,]36 118  8.0   72 5   2
[3,]12 149 12.6   74 5   3
[4,]18 313 11.5   62 5   4
[5,]NA  NA 14.3   56 5   5
[6,]28  NA 14.9   66 5   6
[7,]23 299  8.6   65 5   7
[8,]19  99 13.8   59 5   8

Z - Y[,complete.cases(t(Y))==T]

head(Z, n=8)
 Wind Temp Month Day
[1,]  7.4   67 5   1
[2,]  8.0   72 5   2
[3,] 12.6   74 5   3
[4,] 11.5   62 5   4
[5,] 14.3   56 5   5
[6,] 14.9   66 5   6
[7,]  8.6   65 5   7
[8,] 13.8   59 5   8

The columns that contained NA were deleted.


Le 27 mars 2015 � 10:38, peter dalgaard pda...@gmail.com a �crit :

 
 On 27 Mar 2015, at 09:58 , St�phane Adamowicz 
 stephane.adamow...@avignon.inra.fr wrote:
 
 data_no_NA - data[, complete.cases(t(data))==T]
 
 Ouch! logical == TRUE is bad, logical == T is worse:
 
 data[, complete.cases(t(data))]
 
 
 -- 
 Peter Dalgaard, Professor,
 Center for Statistics, Copenhagen Business School
 Solbjerg Plads 3, 2000 Frederiksberg, Denmark
 Phone: (+45)38153501
 Office: A 4.23
 Email: pd@cbs.dk  Priv: pda...@gmail.com
 
 
 
 
 
 
 
 
 



_
St�phane Adamowicz
Inra, centre de recherche Paca, unit� PSH
228, route de l'a�rodrome
CS 40509
domaine St Paul, site Agroparc
84914 Avignon, cedex 9
France

stephane.adamow...@avignon.inra.fr
tel.  +33 (0)4 32 72 24 35
fax. +33 (0)4 32 72 24 32
do not dial 0 when out of France
web PSH  : https://www6.paca.inra.fr/psh
web Inra : http://www.inra.fr/
_


[[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] matrix manipulation question

2015-03-27 Thread Stéphane Adamowicz
Why not use complete.cases() ?

data_no_NA - data[, complete.cases(t(data))==T]


Le 27 mars 2015 à 06:13, Jatin Kala jatin.kala...@gmail.com a écrit :

 Hi,
 I've got a rather large matrix of about 800 rows and 60 columns.
 Each column is a time-series 800 long.
 
 Out of these 60 time series, some have missing values (NA).
 I want to strip out all columns that have one or more NA values, i.e., only 
 want full time series.
 
 This should do the trick:
 data_no_NA - data[,!apply(is.na(data), 2, any)]
 
 I now use data_no_NA as input to a function, which returns output as a matrix 
 of the same size as data_no_NA
 
 The trick is that i now need to put these columns back into a new 800 by 
 60 empty matrix, at their original locations.
 Any suggestions on how to do that? hopefully without having to use loops.
 I'm using R/3.0.3
 
 Cheers,
 Jatin.
 
 __
 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] matrix manipulation question

2015-03-27 Thread Stéphane Adamowicz

Le 27 mars 2015 à 12:34, PIKAL Petr petr.pi...@precheza.cz a écrit :

 Very, very, very bad solution.
  
 as.matrix can change silently your data to unwanted format, 
 complete.cases()==T is silly as Peter already pointed out.
  
 

Perhaps, but it happens that in the original message, the question dealt with a 
matrix not a dataframe, and thus I needed a matrix example. Furthermore in my 
example no unwanted format occurred. You can check easily that the final matrix 
contains only « numeric » data as in the original data frame.

Stéphane
  
  
  
  
  
 
 
 
 _
 St�phane Adamowicz
 Inra, centre de recherche Paca, unit� PSH
 228, route de l'a�rodrome
 CS 40509
 domaine St Paul, site Agroparc
 84914 Avignon, cedex 9
 France
 
 stephane.adamow...@avignon.inra.fr
 tel.  +33 (0)4 32 72 24 35
 fax. +33 (0)4 32 72 24 32
 do not dial 0 when out of France
 web PSH  : https://www6.paca.inra.fr/psh
 web Inra : http://www.inra.fr/
 _
 
 
 [[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.
 
 Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou 
 určeny pouze jeho adresátům.
 Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
 jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
 svého systému.
 Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
 jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
 Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
 zpožděním přenosu e-mailu.
 
 V případě, že je tento e-mail součástí obchodního jednání:
 - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, 
 a to z jakéhokoliv důvodu i bez uvedení důvodu.
 - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
 Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany 
 příjemce s dodatkem či odchylkou.
 - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
 dosažením shody na všech jejích náležitostech.
 - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
 žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
 pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu 
 případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je 
 adresátovi či osobě jím zastoupené známá.
 
 This e-mail and any documents attached to it may be confidential and are 
 intended only for its intended recipients.
 If you received this e-mail by mistake, please immediately inform its sender. 
 Delete the contents of this e-mail with all attachments and its copies from 
 your system.
 If you are not the intended recipient of this e-mail, you are not authorized 
 to use, disseminate, copy or disclose this e-mail in any manner.
 The sender of this e-mail shall not be liable for any possible damage caused 
 by modifications of the e-mail or by delay with transfer of the email.
 
 In case that this e-mail forms part of business dealings:
 - the sender reserves the right to end negotiations about entering into a 
 contract in any time, for any reason, and without stating any reasoning.
 - if the e-mail contains an offer, the recipient is entitled to immediately 
 accept such offer; The sender of this e-mail (offer) excludes any acceptance 
 of the offer on the part of the recipient containing any amendment or 
 variation.
 - the sender insists on that the respective contract is concluded only upon 
 an express mutual agreement on all its aspects.
 - the sender of this e-mail informs that he/she is not authorized to enter 
 into any contracts on behalf of the company except for cases in which he/she 
 is expressly authorized to do so in writing, and such authorization or power 
 of attorney is submitted to the recipient or the person represented by the 
 recipient, or the existence of such authorization is known to the recipient 
 of the person represented by the recipient.

__
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] matrix manipulation question

2015-03-27 Thread PIKAL Petr
Hi

 -Original Message-
 From: Stéphane Adamowicz [mailto:stephane.adamow...@avignon.inra.fr]
 Sent: Friday, March 27, 2015 1:26 PM
 To: PIKAL Petr
 Cc: peter dalgaard; r-help@r-project.org
 Subject: Re: [R] matrix manipulation question


 Le 27 mars 2015 à 12:34, PIKAL Petr petr.pi...@precheza.cz a écrit :

  Very, very, very bad solution.
 
  as.matrix can change silently your data to unwanted format,
 complete.cases()==T is silly as Peter already pointed out.
 
 

 Perhaps, but it happens that in the original message, the question

I do not have original message.

 dealt with a matrix not a dataframe, and thus I needed a matrix

But you made matrix from data frame. If one column was not numeric all 
resulting matrix would chnge to nonnumeric format.

 example. Furthermore in my example no unwanted format occurred. You can

Yes because data.frame was (luckily) numeric.

 check easily that the final matrix contains only « numeric » data as in
 the original data frame.

You want matrix? Here it is.

 head(as.matrix(airquality)[ ,colSums(is.na(airquality))==0])
 Wind Temp Month Day
[1,]  7.4   67 5   1
[2,]  8.0   72 5   2
[3,] 12.6   74 5   3
[4,] 11.5   62 5   4
[5,] 14.3   56 5   5
[6,] 14.9   66 5   6

Works same with matrix as with data frame without need to transform it.

Cheers
Petr


 Stéphane
  
  
  
  
  
 
 
 
  _
  St phane Adamowicz
  Inra, centre de recherche Paca, unit  PSH 228, route de l'a rodrome
 CS
  40509 domaine St Paul, site Agroparc
  84914 Avignon, cedex 9
  France
 
  stephane.adamow...@avignon.inra.fr
  tel.  +33 (0)4 32 72 24 35
  fax. +33 (0)4 32 72 24 32
  do not dial 0 when out of France
  web PSH  : https://www6.paca.inra.fr/psh web Inra :
  http://www.inra.fr/ _
 
 
  [[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.
 
  Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a
 jsou určeny pouze jeho adresátům.
  Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě
 neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho
 kopie vymažte ze svého systému.
  Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento
 email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
  Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou
 modifikacemi či zpožděním přenosu e-mailu.
 
  V případě, že je tento e-mail součástí obchodního jednání:
  - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření
 smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
  - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně
 přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky
 ze strany příjemce s dodatkem či odchylkou.
  - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve
 výslovným dosažením shody na všech jejích náležitostech.
  - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za
 společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně
 zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly
 adresátovi tohoto emailu případně osobě, kterou adresát zastupuje,
 předloženy nebo jejich existence je adresátovi či osobě jím zastoupené
 známá.
 
  This e-mail and any documents attached to it may be confidential and
 are intended only for its intended recipients.
  If you received this e-mail by mistake, please immediately inform its
 sender. Delete the contents of this e-mail with all attachments and its
 copies from your system.
  If you are not the intended recipient of this e-mail, you are not
 authorized to use, disseminate, copy or disclose this e-mail in any
 manner.
  The sender of this e-mail shall not be liable for any possible damage
 caused by modifications of the e-mail or by delay with transfer of the
 email.
 
  In case that this e-mail forms part of business dealings:
  - the sender reserves the right to end negotiations about entering
 into a contract in any time, for any reason, and without stating any
 reasoning.
  - if the e-mail contains an offer, the recipient is entitled to
 immediately accept such offer; The sender of this e-mail (offer)
 excludes any acceptance of the offer on the part of the recipient
 containing any amendment or variation.
  - the sender insists on that the respective contract is concluded
 only upon an express mutual agreement on all its aspects.
  - the sender of this e-mail informs that he/she is not authorized to
 enter into any contracts on behalf of the company except for cases in
 which he/she is expressly authorized to do so in writing, and such
 authorization or power of attorney is submitted

Re: [R] matrix manipulation question

2015-03-27 Thread Stéphane Adamowicz

 
 example. Furthermore in my example no unwanted format occurred. You can
 
 Yes because data.frame was (luckily) numeric.
 

Luck has nothing to do with this. I Chose this example on purpose …

Stéphane

__
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] matrix manipulation question

2015-03-27 Thread David Winsemius

On Mar 27, 2015, at 3:41 AM, Stéphane Adamowicz wrote:

 Well, it seems to work with me.
 

No one is doubting that it worked for you in this instance. What Peter D. was 
criticizing was the construction :

complete.cases(t(Y))==T

... and it was on two bases that it is wrong. The first is that `T` is not 
guaranteed to be TRUE. The second is that the test ==T (or similarly ==TRUE) is 
completely unnecessary because `complete.cases` returns a logical vector and so 
that expression is a waste of time.

(The issue of matrix versus dataframe was raised by someone else.)

-- 
David.


 Y - as.matrix(airquality)
 head(Y, n=8)
 Ozone Solar.R Wind Temp Month Day
 [1,]41 190  7.4   67 5   1
 [2,]36 118  8.0   72 5   2
 [3,]12 149 12.6   74 5   3
 [4,]18 313 11.5   62 5   4
 [5,]NA  NA 14.3   56 5   5
 [6,]28  NA 14.9   66 5   6
 [7,]23 299  8.6   65 5   7
 [8,]19  99 13.8   59 5   8
 
 Z - Y[,complete.cases(t(Y))==T]
 
 head(Z, n=8)
 Wind Temp Month Day
 [1,]  7.4   67 5   1
 [2,]  8.0   72 5   2
 [3,] 12.6   74 5   3
 [4,] 11.5   62 5   4
 [5,] 14.3   56 5   5
 [6,] 14.9   66 5   6
 [7,]  8.6   65 5   7
 [8,] 13.8   59 5   8
 
 The columns that contained NA were deleted.
 
 
 Le 27 mars 2015 à 10:38, peter dalgaard pda...@gmail.com a écrit :
 
 
 On 27 Mar 2015, at 09:58 , Stéphane Adamowicz 
 stephane.adamow...@avignon.inra.fr wrote:
 
 data_no_NA - data[, complete.cases(t(data))==T]
 
 Ouch! logical == TRUE is bad, logical == T is worse:
 
 data[, complete.cases(t(data))]
 
 
 -- 
 Peter Dalgaard, Professor,
 Center for Statistics, Copenhagen Business School
 Solbjerg Plads 3, 2000 Frederiksberg, Denmark
 Phone: (+45)38153501
 Office: A 4.23
 Email: pd@cbs.dk  Priv: pda...@gmail.com
 
 
 
 
 
 
 
 
 
 
 
 
 _
 Stéphane Adamowicz
 Inra, centre de recherche Paca, unité PSH
 228, route de l'aérodrome
 CS 40509
 domaine St Paul, site Agroparc
 84914 Avignon, cedex 9
 France
 
 stephane.adamow...@avignon.inra.fr
 tel.  +33 (0)4 32 72 24 35
 fax. +33 (0)4 32 72 24 32
 do not dial 0 when out of France
 web PSH  : https://www6.paca.inra.fr/psh
 web Inra : http://www.inra.fr/
 _
 
 
   [[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.

David Winsemius
Alameda, CA, USA

__
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] matrix manipulation question

2015-03-27 Thread Henric Winell

On 2015-03-27 11:41, Stéphane Adamowicz wrote:


Well, it seems to work with me.

Y - as.matrix(airquality)
head(Y, n=8)
  Ozone Solar.R Wind Temp Month Day
[1,]41 190  7.4   67 5   1
[2,]36 118  8.0   72 5   2
[3,]12 149 12.6   74 5   3
[4,]18 313 11.5   62 5   4
[5,]NA  NA 14.3   56 5   5
[6,]28  NA 14.9   66 5   6
[7,]23 299  8.6   65 5   7
[8,]19  99 13.8   59 5   8

Z - Y[,complete.cases(t(Y))==T]


Peter's point, I guess, is that

1. complete.cases(t(Y)) is already a vector of logicals
2. T (and F) can be redefined, so what if T - FALSE?


Henric Winell





head(Z, n=8)
  Wind Temp Month Day
[1,]  7.4   67 5   1
[2,]  8.0   72 5   2
[3,] 12.6   74 5   3
[4,] 11.5   62 5   4
[5,] 14.3   56 5   5
[6,] 14.9   66 5   6
[7,]  8.6   65 5   7
[8,] 13.8   59 5   8

The columns that contained NA were deleted.


Le 27 mars 2015 � 10:38, peter dalgaard pda...@gmail.com a �crit :



On 27 Mar 2015, at 09:58 , St�phane Adamowicz 
stephane.adamow...@avignon.inra.fr wrote:


data_no_NA - data[, complete.cases(t(data))==T]


Ouch! logical == TRUE is bad, logical == T is worse:

data[, complete.cases(t(data))]


--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com













_
St�phane Adamowicz
Inra, centre de recherche Paca, unit� PSH
228, route de l'a�rodrome
CS 40509
domaine St Paul, site Agroparc
84914 Avignon, cedex 9
France

stephane.adamow...@avignon.inra.fr
tel.  +33 (0)4 32 72 24 35
fax. +33 (0)4 32 72 24 32
do not dial 0 when out of France
web PSH  : https://www6.paca.inra.fr/psh
web Inra : http://www.inra.fr/
_


[[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] matrix manipulation question

2015-03-27 Thread Jatin Kala

Thanks Richard,
This works, rather obvious now that i think of it!
=)

On 27/03/2015 4:30 pm, Richard M. Heiberger wrote:

just reverse what you did before.

newdata - data
newdata[] - NA
newdata[,!apply(is.na(data), 2, any)] - myfunction(data_no_NA)

On Fri, Mar 27, 2015 at 1:13 AM, Jatin Kala jatin.kala...@gmail.com wrote:

Hi,
I've got a rather large matrix of about 800 rows and 60 columns.
Each column is a time-series 800 long.

Out of these 60 time series, some have missing values (NA).
I want to strip out all columns that have one or more NA values, i.e., only
want full time series.

This should do the trick:
data_no_NA - data[,!apply(is.na(data), 2, any)]

I now use data_no_NA as input to a function, which returns output as a
matrix of the same size as data_no_NA

The trick is that i now need to put these columns back into a new 800 by
60 empty matrix, at their original locations.
Any suggestions on how to do that? hopefully without having to use loops.
I'm using R/3.0.3

Cheers,
Jatin.

__
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] matrix manipulation question

2015-03-26 Thread Richard M. Heiberger
just reverse what you did before.

newdata - data
newdata[] - NA
newdata[,!apply(is.na(data), 2, any)] - myfunction(data_no_NA)

On Fri, Mar 27, 2015 at 1:13 AM, Jatin Kala jatin.kala...@gmail.com wrote:
 Hi,
 I've got a rather large matrix of about 800 rows and 60 columns.
 Each column is a time-series 800 long.

 Out of these 60 time series, some have missing values (NA).
 I want to strip out all columns that have one or more NA values, i.e., only
 want full time series.

 This should do the trick:
 data_no_NA - data[,!apply(is.na(data), 2, any)]

 I now use data_no_NA as input to a function, which returns output as a
 matrix of the same size as data_no_NA

 The trick is that i now need to put these columns back into a new 800 by
 60 empty matrix, at their original locations.
 Any suggestions on how to do that? hopefully without having to use loops.
 I'm using R/3.0.3

 Cheers,
 Jatin.

 __
 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] Matrix element-by-element multiplication

2015-01-07 Thread John McKown
On Wed, Jan 7, 2015 at 5:05 PM, Steven Yen sye...@gmail.com wrote:

 I like to multiple the first and second column of a 10 x 3 matrix by 100.
 The following did not work. I need this in an operation with a much larger
 scale. Any help?

 aa-matrix(1:30,nrow=10,ncol=3); aa
 bb-matrix(c(100,100,1),nrow=1,ncol=3); bb
 dim(aa)
 dim(bb)
 aa*bb

 Results:

  aa-matrix(1:30,nrow=10,ncol=3); aa
   [,1] [,2] [,3]
  [1,]1   11   21
  [2,]2   12   22
  [3,]3   13   23
  [4,]4   14   24
  [5,]5   15   25
  [6,]6   16   26
  [7,]7   17   27
  [8,]8   18   28
  [9,]9   19   29
 [10,]   10   20   30
  bb-matrix(c(100,100,1),nrow=1,ncol=3); bb
  [,1] [,2] [,3]
 [1,]  100  1001
  dim(aa)
 [1] 10  3
  dim(bb)
 [1] 1 3
  aa*bb
 Error in aa * bb : non-conformable arrays

 


​Assuming that this is exactly what you want to do, then

aa[,1:2]-aa[,1:2]*100;

transcript:

 aa-matrix(1:30,nrow=10,ncol=3);  aa  [,1] [,2] [,3]
 [1,]1   11   21
 [2,]2   12   22
 [3,]3   13   23
 [4,]4   14   24
 [5,]5   15   25
 [6,]6   16   26
 [7,]7   17   27
 [8,]8   18   28
 [9,]9   19   29
[10,]   10   20   30 aa[,1:2]-aa[,1:2]*100 aa  [,1] [,2] [,3]
 [1,]  100 1100   21
 [2,]  200 1200   22
 [3,]  300 1300   23
 [4,]  400 1400   24
 [5,]  500 1500   25
 [6,]  600 1600   26
 [7,]  700 1700   27
 [8,]  800 1800   28
 [9,]  900 1900   29
[10,] 1000 2000   30


​


-- 
​
While a transcendent vocabulary is laudable, one must be eternally careful
so that the calculated objective of communication does not become ensconced
in obscurity.  In other words, eschew obfuscation.

111,111,111 x 111,111,111 = 12,345,678,987,654,321

Maranatha! 
John McKown

[[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] Matrix element-by-element multiplication

2015-01-07 Thread Peter Langfelder
You can create a suitable matrix bb as below (note the byrow = TRUE argument)

aa-matrix(1:30,nrow=10,ncol=3); aa
bb-matrix(c(100,100,1),nrow=10,ncol=3, byrow = TRUE); bb
dim(aa)
dim(bb)
aa * bb


You can also use matrix multiplication, but that;s slightly more involved:

aa-matrix(1:30,nrow=10,ncol=3); aa
bb-matrix(0,nrow=3,ncol=3);
diag(bb) = c(100,100,1);
bb
dim(aa)
dim(bb)
aa %*% bb


HTH,

Peter



On Wed, Jan 7, 2015 at 3:05 PM, Steven Yen sye...@gmail.com wrote:
 I like to multiple the first and second column of a 10 x 3 matrix by 100.
 The following did not work. I need this in an operation with a much larger
 scale. Any help?

 aa-matrix(1:30,nrow=10,ncol=3); aa
 bb-matrix(c(100,100,1),nrow=1,ncol=3); bb
 dim(aa)
 dim(bb)
 aa*bb

 Results:

 aa-matrix(1:30,nrow=10,ncol=3); aa
   [,1] [,2] [,3]
  [1,]1   11   21
  [2,]2   12   22
  [3,]3   13   23
  [4,]4   14   24
  [5,]5   15   25
  [6,]6   16   26
  [7,]7   17   27
  [8,]8   18   28
  [9,]9   19   29
 [10,]   10   20   30
 bb-matrix(c(100,100,1),nrow=1,ncol=3); bb
  [,1] [,2] [,3]
 [1,]  100  1001
 dim(aa)
 [1] 10  3
 dim(bb)
 [1] 1 3
 aa*bb
 Error in aa * bb : non-conformable arrays



 __
 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] Matrix element-by-element multiplication

2015-01-07 Thread Peter Langfelder
On Wed, Jan 7, 2015 at 3:15 PM, Peter Langfelder
peter.langfel...@gmail.com wrote:
 You can create a suitable matrix bb as below (note the byrow = TRUE argument)

 aa-matrix(1:30,nrow=10,ncol=3); aa
 bb-matrix(c(100,100,1),nrow=10,ncol=3, byrow = TRUE); bb
 dim(aa)
 dim(bb)
 aa * bb


 You can also use matrix multiplication, but that;s slightly more involved:

I should add that it will also be much slower if, as you say, you do
it on a much larger scale and the dimensions of bb are large.

Peter

__
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] Matrix element-by-element multiplication

2015-01-07 Thread Steven Yen
Thank you both. Both John and Peter's suggestions work great!!

At 06:17 PM 1/7/2015, John McKown wrote:
On Wed, Jan 7, 2015 at 5:05 PM, Steven Yen 
mailto:sye...@gmail.comsye...@gmail.com wrote:
I like to multiple the first and second column 
of a 10 x 3 matrix by 100. The following did not 
work. I need this in an operation with a much larger scale. Any help?

aa-matrix(1:30,nrow=10,ncol=3); aa
bb-matrix(c(100,100,1),nrow=1,ncol=3); bb
dim(aa)
dim(bb)
aa*bb

Results:

  aa-matrix(1:30,nrow=10,ncol=3); aa
�  �  �  [,1] [,2] [,3]
� [1,]�  �  1�  � 11�  � 21
� [2,]�  �  2�  � 12�  � 22
� [3,]�  �  3�  � 13�  � 23
� [4,]�  �  4�  � 14�  � 24
� [5,]�  �  5�  � 15�  � 25
� [6,]�  �  6�  � 16�  � 26
� [7,]�  �  7�  � 17�  � 27
� [8,]�  �  8�  � 18�  � 28
� [9,]�  �  9�  � 19�  � 29
[10,]�  � 10�  � 20�  � 30
  bb-matrix(c(100,100,1),nrow=1,ncol=3); bb
�  �  � [,1] [,2] [,3]
[1,]�  100�  100�  �  1
  dim(aa)
[1] 10�  3
  dim(bb)
[1] 1 3
  aa*bb
Error in aa * bb : non-conformable arrays

 


​Assuming that this is exactly what you want to do, then�

aa[,1:2]-aa[,1:2]*100;

transcript:

  aa-matrix(1:30,nrow=10,ncol=3);
  aa
   [,1] [,2] [,3]
  [1,]1   11   21
  [2,]2   12   22
  [3,]3   13   23
  [4,]4   14   24
  [5,]5   15   25
  [6,]6   16   26
  [7,]7   17   27
  [8,]8   18   28
  [9,]9   19   29
[10,]   10   20   30
  aa[,1:2]-aa[,1:2]*100
  aa
   [,1] [,2] [,3]
  [1,]  100 1100   21
  [2,]  200 1200   22
  [3,]  300 1300   23
  [4,]  400 1400   24
  [5,]  500 1500   25
  [6,]  600 1600   26
  [7,]  700 1700   27
  [8,]  800 1800   28
  [9,]  900 1900   29
[10,] 1000 2000   30

 
​
�

--
​
While a transcendent vocabulary is laudable, one 
must be eternally careful so that the calculated 
objective of communication does not become 
ensconced in obscurity.�  In other words, eschew obfuscation.

111,111,111 x 111,111,111 = 12,345,678,987,654,321

Maranatha! 
John McKown

[[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] matrix

2014-07-01 Thread arun
Hi Izhak,
If the position of the elements to be replaced follow the pattern below:
seq(1,length(t), by=7)
#[1]  1  8 15

t[seq(1,length(t), by=7)] - c(50,90,100)
A.K.






On Monday, June 30, 2014 4:19 PM, Adams, Jean jvad...@usgs.gov wrote:
t[1, 1] - 50
t[3, 2] - 90
t[5, 3] - 100

Jean


On Mon, Jun 30, 2014 at 10:27 AM, IZHAK shabsogh ishaqb...@yahoo.com
wrote:



 kindly guide me on how i can delete and replace an element from a matrix t
 below

 for example delete first element in column one and replace it with 50,
 third element in column 2 by 90 and fifth element in column 3 by 100


 t1-c(1,2,3,4,5)
 t2-c(6,7,8,9,10)
 t3-c(11,12,13,14,15)
 t-cbind(t1,t2,t3)


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


    [[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] matrix

2014-06-30 Thread Adams, Jean
t[1, 1] - 50
t[3, 2] - 90
t[5, 3] - 100

Jean


On Mon, Jun 30, 2014 at 10:27 AM, IZHAK shabsogh ishaqb...@yahoo.com
wrote:



 kindly guide me on how i can delete and replace an element from a matrix t
 below

 for example delete first element in column one and replace it with 50,
 third element in column 2 by 90 and fifth element in column 3 by 100


 t1-c(1,2,3,4,5)
 t2-c(6,7,8,9,10)
 t3-c(11,12,13,14,15)
 t-cbind(t1,t2,t3)


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


[[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] matrix column division by vector

2014-05-14 Thread David L Carlson
You don't need do.call:

 t(t(m)/v)
 [,1] [,2] [,3]
[1,]222
[2,]111

 t(apply(m, 1, function(x) x/v))
 [,1] [,2] [,3]
[1,]222
[2,]111

-
David L Carlson
Department of Anthropology
Texas AM University
College Station, TX 77840-4352


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of carol white
Sent: Wednesday, May 14, 2014 9:52 AM
To: r-h...@stat.math.ethz.ch
Subject: [R] matrix column division by vector

Hi,
What is the elegant script to divide the columns of a matrix by the respective 
position of a vector elements?

m=rbind(c(6,4,2),c(3,2,1))

v= c(3,2,1)

res= 6/3   4/2  2/1
    3/3   2/2    1/1


this is correct 
mat2 = NULL

for (i in 1: ncol(m))

    mat2 = cbind(mat2, m[,i]/ v[i])


but how to do more compact and elegant with for ex do.call?

Many thanks

Carol
[[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] matrix column division by vector

2014-05-14 Thread Frede Aakmann Tøgersen
Have a look at ?sweep

Br. Frede


Sendt fra Samsung mobil
 Oprindelig meddelelse 
Fra: carol white
Dato:14/05/2014 16.53 (GMT+01:00)
Til: r-h...@stat.math.ethz.ch
Emne: [R] matrix column division by vector

Hi,
What is the elegant script to divide the columns of a matrix by the respective 
position of a vector elements?

m=rbind(c(6,4,2),c(3,2,1))

v= c(3,2,1)

res= 6/3   4/2  2/1
3/3   2/21/1


this is correct
mat2 = NULL

for (i in 1: ncol(m))

mat2 = cbind(mat2, m[,i]/ v[i])


but how to do more compact and elegant with for ex do.call?

Many thanks

Carol
[[alternative HTML version deleted]]


[[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] matrix column division by vector

2014-05-14 Thread Bert Gunter
Oh my goodness David! Don't forget that matrices are just vectors in
column major order.


 m/rep(v,e=2)
 [,1] [,2] [,3]
[1,]222
[2,]111

(the generalization is obvious)

should be far more efficient and simpler  than running apply loops.

-- Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom.
H. Gilbert Welch




On Wed, May 14, 2014 at 7:59 AM, David L Carlson dcarl...@tamu.edu wrote:
 You don't need do.call:

 t(t(m)/v)
  [,1] [,2] [,3]
 [1,]222
 [2,]111

 t(apply(m, 1, function(x) x/v))
  [,1] [,2] [,3]
 [1,]222
 [2,]111

 -
 David L Carlson
 Department of Anthropology
 Texas AM University
 College Station, TX 77840-4352


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf Of carol white
 Sent: Wednesday, May 14, 2014 9:52 AM
 To: r-h...@stat.math.ethz.ch
 Subject: [R] matrix column division by vector

 Hi,
 What is the elegant script to divide the columns of a matrix by the 
 respective position of a vector elements?

 m=rbind(c(6,4,2),c(3,2,1))

 v= c(3,2,1)

 res= 6/3   4/2  2/1
 3/3   2/21/1


 this is correct
 mat2 = NULL

 for (i in 1: ncol(m))

 mat2 = cbind(mat2, m[,i]/ v[i])


 but how to do more compact and elegant with for ex do.call?

 Many thanks

 Carol
 [[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] matrix column division by vector

2014-05-14 Thread Jeff Newmiller
Please post in plain text... your email is getting distorted and hard to read 
by the HTML.

I don't know how to use do.call for this, but when you understand how vectors 
recycle and matrices and arrays are laid out in memory (read the Introduction 
to R document if not) then the following comes to mind:

mat2 - m / matrix( v, ncol=ncol(m), nrow=nrow(m), byrow=TRUE )

---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On May 14, 2014 7:51:36 AM PDT, carol white wht_...@yahoo.com wrote:
Hi,
What is the elegant script to divide the columns of a matrix by the
respective position of a vector elements?

m=rbind(c(6,4,2),c(3,2,1))

v= c(3,2,1)

res= 6/3�� 4/2� 2/1
��� 3/3�� 2/2 �� 1/1


this is correct�
mat2 = NULL

for (i in 1: ncol(m))

��� mat2 = cbind(mat2, m[,i]/ v[i])


but how to do more compact and elegant with for ex do.call?

Many thanks

Carol
   [[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] matrix column division by vector

2014-05-14 Thread David L Carlson
Bert wins the race:

 system.time(replicate(1e5, m/rep(v,e=2)))
   user  system elapsed 
   0.250.000.25 
 system.time(replicate(1e5, m/matrix( v, ncol=ncol(m), nrow=nrow(m), 
 byrow=TRUE)))
   user  system elapsed 
   0.420.000.42 
 system.time(replicate(1e5, t(t(m)/v)))
   user  system elapsed 
   1.310.001.33 
 system.time(replicate(1e5, sweep(m, 2, v, /)))
   user  system elapsed 
   3.390.003.40 
 system.time(replicate(1e5, t(apply(m, 1, function(x) x/v
   user  system elapsed 
   5.040.015.06

David C

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Jeff Newmiller
Sent: Wednesday, May 14, 2014 10:28 AM
To: carol white; carol white; r-h...@stat.math.ethz.ch
Subject: Re: [R] matrix column division by vector

Please post in plain text... your email is getting distorted and hard to read 
by the HTML.

I don't know how to use do.call for this, but when you understand how vectors 
recycle and matrices and arrays are laid out in memory (read the Introduction 
to R document if not) then the following comes to mind:

mat2 - m / matrix( v, ncol=ncol(m), nrow=nrow(m), byrow=TRUE )

---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On May 14, 2014 7:51:36 AM PDT, carol white wht_...@yahoo.com wrote:
Hi,
What is the elegant script to divide the columns of a matrix by the
respective position of a vector elements?

m=rbind(c(6,4,2),c(3,2,1))

v= c(3,2,1)

res= 6/3�� 4/2� 2/1
��� 3/3�� 2/2 �� 1/1


this is correct�
mat2 = NULL

for (i in 1: ncol(m))

��� mat2 = cbind(mat2, m[,i]/ v[i])


but how to do more compact and elegant with for ex do.call?

Many thanks

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


  1   2   3   4   5   6   7   >