Re: [R] struggling with apply

2020-05-27 Thread Bert Gunter
Yes, that's better --- no looping at the interpreted level. Another version without transposing is: nr <- 3 matrix(pmin(c(somematrix),rep(UB, e = nr)), nrow = nr) Both treat the matrix as a vector stored in column major order. Cheers, Bert On Wed, May 27, 2020 at 1:32 PM Mathew Guilfoyle wro

Re: [R] struggling with apply

2020-05-27 Thread Michael Ashton
This is like "Name that Tune." Can anyone do it in FEWER characters? :-) On May 27, 2020, at 4:32 PM, Mathew Guilfoyle wrote:  A bit quicker: t(pmin(t(somematrix), UB)) On 27 May 2020, at 20:56, Bert Gunter mailto:bgunter.4...@gmail.com>> wrote: Jeff: Check it! somematrix <- matrix(c(1,4

Re: [R] struggling with apply

2020-05-27 Thread Mathew Guilfoyle
A bit quicker: t(pmin(t(somematrix), UB)) > On 27 May 2020, at 20:56, Bert Gunter wrote: > > Jeff: Check it! > >> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4) >> UB=c(2.5, 5.5, 8.5, 10.5) >> apply( somematrix, 2, function( x ) pmin( x, UB ) ) > [,1] [,2] [,3] [,4] >

Re: [R] struggling with apply

2020-05-27 Thread Bert Gunter
Jeff: Check it! > somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4) > UB=c(2.5, 5.5, 8.5, 10.5) > apply( somematrix, 2, function( x ) pmin( x, UB ) ) [,1] [,2] [,3] [,4] [1,]1 2.5 2.5 2.5 [2,]4 3.0 5.5 5.5 [3,]3 8.5 5.0 8.5 [4,]1 6.0 10.5 7.0 Not wh

Re: [R] struggling with apply

2020-05-27 Thread Jeff Newmiller
Sigh. Transpose? apply( somematrix, 2, function( x ) pmin( x, UB ) ) On May 27, 2020 11:22:06 AM PDT, Bert Gunter wrote: >Better, I think (no indexing): > >t(apply(somematrix,1,function(x)pmin(x,UB))) > > >Bert Gunter > >"The trouble with having an open mind is that people keep coming along >and

Re: [R] struggling with apply

2020-05-27 Thread Michael Ashton
://calendly.com/m-ashton From: Bert Gunter [mailto:bgunter.4...@gmail.com] Sent: Wednesday, May 27, 2020 2:22 PM To: Rui Barradas Cc: Michael Ashton; r-help@r-project.org Subject: Re: [R] struggling with apply Better, I think (no indexing): t(apply(somematrix,1,function(x)pmin(x,UB))) Bert Gunter

Re: [R] struggling with apply

2020-05-27 Thread Bert Gunter
Better, I think (no indexing): t(apply(somematrix,1,function(x)pmin(x,UB))) 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 Wed, May 27, 2020 at 10:56 AM Rui

Re: [R] struggling with apply

2020-05-27 Thread Michael Ashton
ay 27, 2020 1:51 PM To: Michael Ashton; r-help@r-project.org Subject: Re: [R] struggling with apply Hello, Try pmin. And loop by column/UB index with sapply/seq_along. sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i])) # [,1] [,2] [,3] [,4] #[1,] 1.0 5.5 8.5 7.0 #[2,] 2.5

Re: [R] struggling with apply

2020-05-27 Thread Rui Barradas
Hello, Try pmin. And loop by column/UB index with sapply/seq_along. sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i])) # [,1] [,2] [,3] [,4] #[1,] 1.0 5.5 8.5 7.0 #[2,] 2.5 3.0 8.0 10.5 #[3,] 2.5 5.5 5.0 10.5 Hope this helps, Rui Barradas Às 18:46 de 27/05/20, Mic

Re: [R] struggling with apply

2020-05-27 Thread Jeff Newmiller
Use the pmin function, not min, for this purpose. On May 27, 2020 10:46:13 AM PDT, Michael Ashton wrote: >Hi - > >I have a matrix of n rows and 4 columns. > >I want to cap the value in each column by a different upper bound. So, >suppose my matrix is > >somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,