Re: [R] [External] converting MATLAB -> R | element-wise operation

2024-02-29 Thread Evan Cooch
Very interesting - thanks! Most of my problems are not limited by 
compute speed, but its clear that for some sorts of compute-intensive 
problems, sweep might be a limiting approach.

On 2/29/2024 6:12 PM, Richard M. Heiberger wrote:
> I decided to do a direct comparison of transpose and sweep.
>
>
> library(microbenchmark)
>
> NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE)  # Example matrix
> lambda <- c(2, 3, 4)  # Example vector
> colNN <- t(NN)
>
> microbenchmark(
>sweep = sweep(NN, 2, lambda, "/"),
>transpose = t(t(NN)/lambda),
>colNN = colNN/lambda
> )
>
>
> Unit: nanoseconds
>expr   minlq mean median  uq   max neval cld
>   sweep 13817 14145 15115.06  14350 14657.5 75932   100 a
>   transpose  1845  1927  2151.68   2132  2214.0  7093   100  b
>   colNN82   123   141.86123   164.0   492   100   c
>
> Note that transpose is much faster than sweep because it is doing less work,
> I believe essentially just changing the order of indexing.
>
> Using the natural sequencing for column-ordered matrices is much much faster.
>
>> On Feb 28, 2024, at 18:43, peter dalgaard  wrote:
>>
>>> rbind(1:3,4:6)/t(matrix(c(2,3,4), 3,2))
>

[[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] [External] converting MATLAB -> R | element-wise operation

2024-02-29 Thread Richard M. Heiberger
I added two more rows

library(microbenchmark)

NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE)  # Example matrix
lambda <- c(2, 3, 4)  # Example vector
colNN <- t(NN)
matlam <- matrix(lambda, byrow=TRUE, nrow=2, ncol=3)

microbenchmark(
  sweep = sweep(NN, 2, lambda, "/"),
  transpose = t(t(NN)/lambda),
  colNN = colNN/lambda,
  fullsize  = NN / matrix(lambda, byrow=TRUE, nrow=2, ncol=3),
  rowlam  = NN / matlam
)

Unit: nanoseconds
  expr   minlq mean  median  uq   max neval cld
 sweep 12546 12792 13919.91 12997.0 13325.0 85608   100 a
 transpose  1640  1763  1986.04  1947.5  2050.0  7462   100  b
 colNN8282   161.13   123.0   123.0  3854   100   c
  fullsize   738   820   932.34   881.5   963.5  2829   100  bc
rowlam82   123   168.92   164.0   164.0   820   100   c

reshaping the denominator to the correct size in advance is very helpful if you 
will be doing this division more than once.



> On Feb 29, 2024, at 18:12, Richard M. Heiberger  wrote:
>
> I decided to do a direct comparison of transpose and sweep.
>
>
> library(microbenchmark)
>
> NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE)  # Example matrix
> lambda <- c(2, 3, 4)  # Example vector
> colNN <- t(NN)
>
> microbenchmark(
>  sweep = sweep(NN, 2, lambda, "/"),
>  transpose = t(t(NN)/lambda),
>  colNN = colNN/lambda
> )
>
>
> Unit: nanoseconds
>  expr   minlq mean median  uq   max neval cld
> sweep 13817 14145 15115.06  14350 14657.5 75932   100 a
> transpose  1845  1927  2151.68   2132  2214.0  7093   100  b
> colNN82   123   141.86123   164.0   492   100   c
>
> Note that transpose is much faster than sweep because it is doing less work,
> I believe essentially just changing the order of indexing.
>
> Using the natural sequencing for column-ordered matrices is much much faster.
>
>> On Feb 28, 2024, at 18:43, peter dalgaard  wrote:
>>
>>> rbind(1:3,4:6)/t(matrix(c(2,3,4), 3,2))
>
> __
> 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] Clustering Functions used by Reverse-Dependencies

2024-02-29 Thread Leo Mada via R-help
Dear Ivan,

Thank you very much for this interesting information.

Regarding:
"For well-behaved packages that declare their dependencies correctly,
parsing the NAMESPACE for importFrom() and import() calls should give
you the explicit imports."

I did learn something new (I am not very experienced in package writing). 
Unfortunately, Roxygen2 as of the current version still suggests to use the 
pkg::fname approach:
"If you are using just a few functions from another package, we recommending 
adding the package to the Imports: field of the DESCRIPTION file and calling 
the functions explicitly using ::, e.g., pkg::fun()."
https://roxygen2.r-lib.org/articles/namespace.html

Regarding analysing the actual code: it is good to know that CMD check has also 
some functionality. I will look into it, when I find some free time.

tools:::.check_packages_used is a few pages of code. On the other hand, the 
help page for codetools::checkUsage is quite cryptic. But it's good to know at 
least where to look.

Sincerely,

Leonard


From: Ivan Krylov 
Sent: Wednesday, February 28, 2024 10:36 AM
To: Leo Mada via R-help 
Cc: Leo Mada 
Subject: Re: [R] Clustering Functions used by Reverse-Dependencies

� Sat, 24 Feb 2024 03:08:26 +
Leo Mada via R-help  �:

> Are there any tools to extract the function names called by
> reverse-dependencies?

For well-behaved packages that declare their dependencies correctly,
parsing the NAMESPACE for importFrom() and import() calls should give
you the explicit imports. (What if the package imports the whole
dependency? The safe assumption is that all functions are used, but it
comes with false positives. You could also walk the package code
looking for function names that may belong to the imported package, but
that may involve both false positives and false negatives.)

For the rest of the imports and uses of weak dependencies, you'll have
to walk the package code looking for the uses of the `::` operator. See
how R CMD check walks the package code in functions
tools:::.check_packages_used and codetools::checkUsage.

A less-well-behaved package can always load a namespace during runtime
and choose the functions to call depending on the phase of the moon or
weather on Jupiter. For these, like for the halting problem, there's no
general solution: the package could be written to say, "if Leonard's
function says I'm about to call foo::bar, I won't do it, otherwise I
will".

--
Best regards,
Ivan

[[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] [External] converting MATLAB -> R | element-wise operation

2024-02-29 Thread Richard M. Heiberger
I decided to do a direct comparison of transpose and sweep.


library(microbenchmark)

NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE)  # Example matrix
lambda <- c(2, 3, 4)  # Example vector
colNN <- t(NN)

microbenchmark(
  sweep = sweep(NN, 2, lambda, "/"),
  transpose = t(t(NN)/lambda),
  colNN = colNN/lambda
)


Unit: nanoseconds
  expr   minlq mean median  uq   max neval cld
 sweep 13817 14145 15115.06  14350 14657.5 75932   100 a  
 transpose  1845  1927  2151.68   2132  2214.0  7093   100  b 
 colNN82   123   141.86123   164.0   492   100   c

Note that transpose is much faster than sweep because it is doing less work,
I believe essentially just changing the order of indexing.

Using the natural sequencing for column-ordered matrices is much much faster.

> On Feb 28, 2024, at 18:43, peter dalgaard  wrote:
> 
>> rbind(1:3,4:6)/t(matrix(c(2,3,4), 3,2))

__
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] R 4.3.3 is released

2024-02-29 Thread Peter Dalgaard via R-announce
The build system rolled up R-4.3.3.tar.gz and .xz (codename "Angel Food Cake") 
this morning.

This is a minor update, intended as the wrap-up release for the 4.3.x series. 

This also marks the 6th anniversary of R-1.0.0. (2000-02-29)

The list below details the changes in this release. 

You can get the source code from

https://cran.r-project.org/src/base/R-4/R-4.3.3.tar.gz
https://cran.r-project.org/src/base/R-4/R-4.3.3.tar.xz

or wait for it to be mirrored at a CRAN site nearer to you.

Binaries for various platforms will appear in due course.


For the R Core Team,

Peter Dalgaard


These are the checksums (md5 and SHA-256) for the freshly created files, in 
case you wish
to check that they are uncorrupted:

MD5 (AUTHORS) = 320967884b547734d6279dedbc739dd4
MD5 (COPYING) = eb723b61539feef013de476e68b5c50a
MD5 (COPYING.LIB) = a6f89e2100d9b6cdffcea4f398e37343
MD5 (FAQ) = 97a3ddc25aab502a70bfb1a79ab6f862
MD5 (INSTALL) = 7893f754308ca31f1ccf62055090ad7b
MD5 (NEWS) = 0aa4babeb5349c3abc6fb02700e8cf53
MD5 (NEWS.0) = bfcd7c147251b5474d96848c6f57e5a8
MD5 (NEWS.1) = 4108ab429e768e29b1c3b418c224246e
MD5 (NEWS.2) = b38d94569700664205a76a7de836ba83
MD5 (NEWS.3) = e55ed2c8a547b827b46e08eb7137ba23
MD5 (R-latest.tar.gz) = 4de100b35e3614c19df5e95e483cc3c3
MD5 (R-latest.tar.xz) = 5602f5996107c346dba12a16e866d2e2
MD5 (README) = f468f281c919665e276a1b691decbbe6
MD5 (RESOURCES) = a79b9b338cab09bd665f6b62ac6f455b
MD5 (THANKS) = 45b6d2e88a6ecb5b24fa33a781351cd5
MD5 (VERSION-INFO.dcf) = becc8fce6e97db1703f9ca6d80e36c9d
MD5 (R-4/R-4.3.3.tar.gz) = 4de100b35e3614c19df5e95e483cc3c3
MD5 (R-4/R-4.3.3.tar.xz) = 5602f5996107c346dba12a16e866d2e2

60a0d150e6fc1f424be76ad7b645d236b56e747692a4679f81ce6536c550e949  AUTHORS
e6d6a009505e345fe949e1310334fcb0747f28dae2856759de102ab66b722cb4  COPYING
6095e9ffa777dd22839f7801aa845b31c9ed07f3d6bf8a26dc5d2dec8ccc0ef3  COPYING.LIB
3a47bca1e2a7db27c0ca12be388c238e2608ff2f768e627650a71a0ffc826038  FAQ
f87461be6cbaecc4dce44ac58e5bd52364b0491ccdadaf846cb9b452e9550f31  INSTALL
f28b88bf20aa2a0078214b89353985680c53092d55f83e59b8295e61ad1150e0  NEWS
4e21b62f515b749f80997063fceab626d7258c7d650e81a662ba8e0640f12f62  NEWS.0
5de7657c5e58e481403c0dd1a74a5c090b3ef481ce75a91dfe05d4b03f63163f  NEWS.1
cde079b6beab7d700d3d4ecda494e2681ad3b7f8fab13b68be090f949393ec62  NEWS.2
1910a2405300b9bc7c76beeb0753a5249cf799afe175ce28f8d782fab723e012  NEWS.3
80851231393b85bf3877ee9e39b282e750ed864c5ec60cbd68e6e139f0520330  
R-latest.tar.gz
9b4c5f4cabab23f38e72fee36d98772c640a97305d06ce6e1a6a73e82b850954  
R-latest.tar.xz
2fdd3e90f23f32692d4b3a0c0452f2c219a10882033d1774f8cadf25886c3ddc  README
8b7d3856100220f4555d4d57140829f2e81c27eccec5b441f5dce616e9ec9061  RESOURCES
8319c5415de58ee10d4bc058d79c370fd8e6b2ad09e25d7a1e04b74ca5f380a6  THANKS
b8c2534c643ffcd942e8df370a4970c913be5dfc24e687bb12d609e974308aef  
VERSION-INFO.dcf
80851231393b85bf3877ee9e39b282e750ed864c5ec60cbd68e6e139f0520330  
R-4/R-4.3.3.tar.gz
9b4c5f4cabab23f38e72fee36d98772c640a97305d06ce6e1a6a73e82b850954  
R-4/R-4.3.3.tar.xz

This is the relevant part of the NEWS file

CHANGES IN R 4.3.3:

  NEW FEATURES:

* iconv() now fixes up variant encoding names such as "utf8"
  case-insensitively.

  DEPRECATED AND DEFUNCT:

* The legacy encoding = "MacRoman" is deprecated in pdf() and
  postscript(): support was incomplete in earlier versions of R.

  BUG FIXES:

* Arguments are now properly forwarded to methods on S4 generics
  with ... in the middle of their formal arguments. This was broken
  for the case when a method introduced an argument but did not
  include ... in its own formals. Thanks to Herv'e Pag`es for the
  report PR#18538.

* Some invalid file arguments to pictex(), postscript() and xfig()
  opened a file called NA rather than throw an error.  These
  included postscript(NULL) (which some people expected to work
  like pdf(NULL)).

* Passing filename = NA to svg(), cairo_pdf(), cairo_ps() or the
  Cairo-based bitmap devices opened a file called NA: it now throws
  an error.

* quartz(file = NA) opened a file called NA, including when used as
  a Quartz-based bitmap device.  It now gives an error.

* rank() now works, fixing PR#18617, thanks to Ilia
  Kats.

* seq.int() did not adequately check its length.out argument.

* match(, .) is correct again for differing time zones,
  ditto for "POSIXlt", fixing PR#18618 reported by Bastian Klein.

* drop.terms(*, dropx = <0-length>) now works, fixing PR#18563 as
  proposed by Mikael Jagan.

* drop.terms(*) keeps + offset(.) terms when it should, PR#18565,
  and drop.terms() no longer makes up a response, PR#18566, fixing
  both bugs thanks to Mikael Jagan.

* getS3method("t", "test") no longer finds the t.test() function,
  fixing PR#18627.

* pdf() and postscript() support for the documented Adobe encodings
  "Greek" and "Cyrilllic" was missing (although the corresponding
  Windows

Re: [R] Initializing vector and matrices

2024-02-29 Thread Steven Yen
Thanks to all. Great ideas. I found Eik Vettorazzi's suggesstion easy to 
implrment:


ebarm<-vbarm<-NULL
...

if (is.null(ebarm)) ebarm<-ame.00$ei/k else ebarm<-ebarm+ame.00$ei/k
if (is.null(vbarm)) vbarm<-ame.00$vi/k else vbarm<-vbarm+ame.00$vi/k
...

Steven Yen

On 2/29/2024 10:31 PM, Ebert,Timothy Aaron wrote:


You could declare a matrix much larger than you intend to use. This works with 
a few megabytes of data. It is not very efficient, so scaling up may become a 
problem.
m22 <- matrix(NA, 1:60, ncol=6)

It does not work to add a new column to the matrix, as in you get an error if 
you try m22[ , 7] but convert to data frame and add a column

m23 <- data.frame(m22)
m23$x7 <- 12

The only penalty that I know of to having unused space in a matrix is the 
amount of memory it takes. One side effect is that your program may have a 
mistake that you would normally catch with a subscript out of bounds error but 
with the extra space it now runs without errors.

Tim



-Original Message-
From: R-help  On Behalf Of Richard O'Keefe
Sent: Thursday, February 29, 2024 5:29 AM
To: Steven Yen 
Cc: R-help Mailing List 
Subject: Re: [R] Initializing vector and matrices

[External Email]

x <- numeric(0)
for (...) {
 x[length(x)+1] <- ...
}
works.
You can build a matrix by building a vector one element at a time this way, and 
then reshaping it at the end.  That only works if you don't need it to be a 
matrix at all times.
Another approach is to build a list of rows.  It's not a matrix, but a list of 
rows can be a *ragged* matrix with rows of varying length.

On Wed, 28 Feb 2024 at 21:57, Steven Yen  wrote:

Is there as way to initialize a vector (matrix) with an unknown length
(dimension)? NULL does not seem to work. The lines below work with a
vector of length 4 and a matrix of 4 x 4. What if I do not know
initially the length/dimension of the vector/matrix?

All I want is to add up (accumulate)  the vector and matrix as I go
through the loop.

Or, are there other ways to accumulate such vectors and matrices?

  > x<-rep(0,4)  # this works but I like to leave the length open  >
for (i in 1:3){
+  x1<-1:4
+  x<-x+x1
+ }
  > x
[1]  3  6  9 12

  > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to
leave the dimension open
   [,1] [,2] [,3] [,4]
[1,]0000
[2,]0000
[3,]0000
[4,]0000
  > for (i in 1:3){
+   y1<-matrix(17:32, nrow = 4, ncol = 4)
+   y<-y+y1
+ }
  > y
   [,1] [,2] [,3] [,4]
[1,]   51   63   75   87
[2,]   54   66   78   90
[3,]   57   69   81   93
[4,]   60   72   84   96
  >

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat/
.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%40ufl.edu
%7Cdbccaccf29674b10b17308dc39114d38%7C0d4da0f84a314d76ace60a62331e1b84
%7C0%7C0%7C638447993707432549%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=
PtWjcDOnwO7PArVOSdgYbpz8ksjDPK%2Bn9ySyhwQC0gE%3D&reserved=0
PLEASE do read the posting guide
http://www.r/
-project.org%2Fposting-guide.html&data=05%7C02%7Ctebert%40ufl.edu%7Cdb
ccaccf29674b10b17308dc39114d38%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
7C0%7C638447993707438911%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=Igb16
CBYgG21HLEDH4I4gfjjFBa3KjDFK8yEZUmBo8s%3D&reserved=0
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] Initializing vector and matrices

2024-02-29 Thread Ebert,Timothy Aaron
You could declare a matrix much larger than you intend to use. This works with 
a few megabytes of data. It is not very efficient, so scaling up may become a 
problem.
m22 <- matrix(NA, 1:60, ncol=6)

It does not work to add a new column to the matrix, as in you get an error if 
you try m22[ , 7] but convert to data frame and add a column

m23 <- data.frame(m22)
m23$x7 <- 12

The only penalty that I know of to having unused space in a matrix is the 
amount of memory it takes. One side effect is that your program may have a 
mistake that you would normally catch with a subscript out of bounds error but 
with the extra space it now runs without errors.

Tim



-Original Message-
From: R-help  On Behalf Of Richard O'Keefe
Sent: Thursday, February 29, 2024 5:29 AM
To: Steven Yen 
Cc: R-help Mailing List 
Subject: Re: [R] Initializing vector and matrices

[External Email]

x <- numeric(0)
for (...) {
x[length(x)+1] <- ...
}
works.
You can build a matrix by building a vector one element at a time this way, and 
then reshaping it at the end.  That only works if you don't need it to be a 
matrix at all times.
Another approach is to build a list of rows.  It's not a matrix, but a list of 
rows can be a *ragged* matrix with rows of varying length.

On Wed, 28 Feb 2024 at 21:57, Steven Yen  wrote:
>
> Is there as way to initialize a vector (matrix) with an unknown length
> (dimension)? NULL does not seem to work. The lines below work with a
> vector of length 4 and a matrix of 4 x 4. What if I do not know
> initially the length/dimension of the vector/matrix?
>
> All I want is to add up (accumulate)  the vector and matrix as I go
> through the loop.
>
> Or, are there other ways to accumulate such vectors and matrices?
>
>  > x<-rep(0,4)  # this works but I like to leave the length open  >
> for (i in 1:3){
> +  x1<-1:4
> +  x<-x+x1
> + }
>  > x
> [1]  3  6  9 12
>
>  > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to
> leave the dimension open
>   [,1] [,2] [,3] [,4]
> [1,]0000
> [2,]0000
> [3,]0000
> [4,]0000
>  > for (i in 1:3){
> +   y1<-matrix(17:32, nrow = 4, ncol = 4)
> +   y<-y+y1
> + }
>  > y
>   [,1] [,2] [,3] [,4]
> [1,]   51   63   75   87
> [2,]   54   66   78   90
> [3,]   57   69   81   93
> [4,]   60   72   84   96
>  >
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat/
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%40ufl.edu
> %7Cdbccaccf29674b10b17308dc39114d38%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638447993707432549%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=
> PtWjcDOnwO7PArVOSdgYbpz8ksjDPK%2Bn9ySyhwQC0gE%3D&reserved=0
> PLEASE do read the posting guide
> http://www.r/
> -project.org%2Fposting-guide.html&data=05%7C02%7Ctebert%40ufl.edu%7Cdb
> ccaccf29674b10b17308dc39114d38%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638447993707438911%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=Igb16
> CBYgG21HLEDH4I4gfjjFBa3KjDFK8yEZUmBo8s%3D&reserved=0
> 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] Initializing vector and matrices

2024-02-29 Thread Richard O'Keefe
x <- numeric(0)
for (...) {
x[length(x)+1] <- ...
}
works.
You can build a matrix by building a vector one element at a time this way,
and then reshaping it at the end.  That only works if you don't need it to be
a matrix at all times.
Another approach is to build a list of rows.  It's not a matrix, but a list of
rows can be a *ragged* matrix with rows of varying length.

On Wed, 28 Feb 2024 at 21:57, Steven Yen  wrote:
>
> Is there as way to initialize a vector (matrix) with an unknown length
> (dimension)? NULL does not seem to work. The lines below work with a
> vector of length 4 and a matrix of 4 x 4. What if I do not know
> initially the length/dimension of the vector/matrix?
>
> All I want is to add up (accumulate)  the vector and matrix as I go
> through the loop.
>
> Or, are there other ways to accumulate such vectors and matrices?
>
>  > x<-rep(0,4)  # this works but I like to leave the length open
>  >  for (i in 1:3){
> +  x1<-1:4
> +  x<-x+x1
> + }
>  > x
> [1]  3  6  9 12
>
>  > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to
> leave the dimension open
>   [,1] [,2] [,3] [,4]
> [1,]0000
> [2,]0000
> [3,]0000
> [4,]0000
>  > for (i in 1:3){
> +   y1<-matrix(17:32, nrow = 4, ncol = 4)
> +   y<-y+y1
> + }
>  > y
>   [,1] [,2] [,3] [,4]
> [1,]   51   63   75   87
> [2,]   54   66   78   90
> [3,]   57   69   81   93
> [4,]   60   72   84   96
>  >
>
> __
> 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] [EXT] Re: Initializing vector and matrices

2024-02-29 Thread Eik Vettorazzi

Dear Steven,
I used "sample" just to generate a non-trivial example, you could insert 
your code of generating the real xi at this point :-)


If you want to stick to for-loops for some reasons, something like this 
could work


x<-NULL
for (i in 1:5){
   xi<-1:5
  if (is.null(x)) x<-xi else x<-x+xi
}


cheers

Am 29.02.2024 um 09:23 schrieb Steven Yen:

Hello Eik:

Thanks. I do not need to sample. Essentially, I have a do loop which 
produces 24 vectors of length of some length (say k=300) and 24 matrices 
of 300x300. Then, I simply need to  take the averages of these 24 
vectors and matrices:


x=(x1+x2+...+x24)/k

y=(y1+y2+...+y24)/k

I am just looking for ways to do this in a do loop, which requires 
initialization (to 0's) of x and y. My struggle is not knowning length 
of x until x1 is produced in the first of the loop. Thanks.


Steven

On 2/28/2024 6:22 PM, Eik Vettorazzi wrote:

Hi Steven,
It's not entirely clear what you actually want to achieve in the end.

As soon as you "know" x1, and assuming that the different "xi" do not 
differ in length in the real application, you know the length of the 
target vector.
Instead of the loop, you can use 'Reduce' without having to initialize 
a starting vector.


# generate sample vectors, put them in a list

xi<-lapply(1:5, \(x)sample(5))

# look at xi
xi

# sum over xi
Reduce("+",xi)

this works also for matrices

# generate sample matrices, put them in a list
Xi<-lapply(1:3, \(x)matrix(sample(16), nrow=4))

# look at them
Xi

# sum over Xi
Reduce("+",Xi)

Hope that helps

Eik


Am 28.02.2024 um 09:56 schrieb Steven Yen:
Is there as way to initialize a vector (matrix) with an unknown 
length (dimension)? NULL does not seem to work. The lines below work 
with a vector of length 4 and a matrix of 4 x 4. What if I do not 
know initially the length/dimension of the vector/matrix?


All I want is to add up (accumulate)  the vector and matrix as I go 
through the loop.


Or, are there other ways to accumulate such vectors and matrices?

 > x<-rep(0,4)  # this works but I like to leave the length open
 >  for (i in 1:3){
+  x1<-1:4
+  x<-x+x1
+ }
 > x
[1]  3  6  9 12

 > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to 
leave the dimension open

  [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    0    0    0
[4,]    0    0    0    0
 > for (i in 1:3){
+   y1<-matrix(17:32, nrow = 4, ncol = 4)
+   y<-y+y1
+ }
 > y
  [,1] [,2] [,3] [,4]
[1,]   51   63   75   87
[2,]   54   66   78   90
[3,]   57   69   81   93
[4,]   60   72   84   96
 >

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




--
Eik Vettorazzi

Universitätsklinikum Hamburg-Eppendorf
Institut für Medizinische Biometrie und Epidemiologie

Christoph-Probst-Weg 1
4. Obergeschoss, Raum 04.1.021.1

20246 Hamburg

Telefon: +49 (0) 40 7410 - 58243
Fax: +49 (0) 40 7410 - 57790

Web: www.uke.de/imbe

Webex: https://webteaching-uke.webex.com/meet/e.vettorazzi


--

_

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; 
Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Christian Gerloff (Vorsitzender), Joachim Prölß, 
Prof. Dr. Blanche Schwappach-Pignataro, Matthias Waldmann (komm.)
_

SAVE PAPER - THINK BEFORE PRINTING
__
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] [EXT] Initializing vector and matrices

2024-02-29 Thread Steven Yen

Hello Eik:

Thanks. I do not need to sample. Essentially, I have a do loop which 
produces 24 vectors of length of some length (say k=300) and 24 matrices 
of 300x300. Then, I simply need to  take the averages of these 24 
vectors and matrices:


x=(x1+x2+...+x24)/k

y=(y1+y2+...+y24)/k

I am just looking for ways to do this in a do loop, which requires 
initialization (to 0's) of x and y. My struggle is not knowning length 
of x until x1 is produced in the first of the loop. Thanks.


Steven

On 2/28/2024 6:22 PM, Eik Vettorazzi wrote:

Hi Steven,
It's not entirely clear what you actually want to achieve in the end.

As soon as you "know" x1, and assuming that the different "xi" do not 
differ in length in the real application, you know the length of the 
target vector.
Instead of the loop, you can use 'Reduce' without having to initialize 
a starting vector.


# generate sample vectors, put them in a list

xi<-lapply(1:5, \(x)sample(5))

# look at xi
xi

# sum over xi
Reduce("+",xi)

this works also for matrices

# generate sample matrices, put them in a list
Xi<-lapply(1:3, \(x)matrix(sample(16), nrow=4))

# look at them
Xi

# sum over Xi
Reduce("+",Xi)

Hope that helps

Eik


Am 28.02.2024 um 09:56 schrieb Steven Yen:
Is there as way to initialize a vector (matrix) with an unknown 
length (dimension)? NULL does not seem to work. The lines below work 
with a vector of length 4 and a matrix of 4 x 4. What if I do not 
know initially the length/dimension of the vector/matrix?


All I want is to add up (accumulate)  the vector and matrix as I go 
through the loop.


Or, are there other ways to accumulate such vectors and matrices?

 > x<-rep(0,4)  # this works but I like to leave the length open
 >  for (i in 1:3){
+  x1<-1:4
+  x<-x+x1
+ }
 > x
[1]  3  6  9 12

 > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to 
leave the dimension open

  [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    0    0    0
[4,]    0    0    0    0
 > for (i in 1:3){
+   y1<-matrix(17:32, nrow = 4, ncol = 4)
+   y<-y+y1
+ }
 > y
  [,1] [,2] [,3] [,4]
[1,]   51   63   75   87
[2,]   54   66   78   90
[3,]   57   69   81   93
[4,]   60   72   84   96
 >

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