Re: [R] Multiplying two vectors of the same size to give a third vector of the same size

2023-06-20 Thread Philip Rhoades via R-help

avi,


On 2023-06-21 12:46, avi.e.gr...@gmail.com wrote:

I was rushing out Phil so let me amend what I wrote. As others noted,
this is fairly beginner stuff. If you have more such questions,
besides reading up, please consider sending questions to the Tutor
mailing list where there is more patience. 



No worries - thanks!



You wanted to change selected small values to 0.0.

So you do not want the code I supplied as illustration as  it removes
small elements making a smaller  vector.

Assume this test:

A <- c(NA, 0.3, 0.6, NA, 0.9)
B <- c(NA, 0.2, 0.6, 0.9, 0.9)

C <- A * B
print(C)

The result at this point is:

[1]   NA 0.06 0.36   NA 0.81

As expected, anything with an NA in either vector will generate an NA
in the componentwise multiplication. Note the second item at 0.06 is
under your threshold of 0.1.

What you want is not this:

Result <- C[C < 0.1]

Result

[1]   NA 0.06   NA

That threw away anything above the threshold.



Yes, I realised that later . .



What you want may be this:

C[C < 0.1] <- 0.0
print(C)

This returns

[1]   NA 0.00 0.36   NA 0.81

Everything that is NA or at or above 0.1 is kept and anything below
0.1 is zeroed and kept.

Of course, if you do not want to keep the NA, that can trivially be 
removed:


C[!is.na(C)]
[1] 0.00 0.36 0.81



I actually got there myself after a bit of experimenting! - but you 
pointed me in the right direction!


Thanks!

Phil.



-Original Message-
From: Philip Rhoades 
Sent: Tuesday, June 20, 2023 1:04 PM
To: avi.e.gr...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] Multiplying two vectors of the same size to give a
third vector of the same size

avi,


On 2023-06-21 01:55, avi.e.gr...@gmail.com wrote:

Phil,

What have you tried. This seems straightforward enough.

Could you clarify what you mean by NULL?



I guess in R in would just be an empty cell? - ie NOT a zero.



In R, it is common to use NA or a more specific version of it.



Ah yes, that would be it I think.



So assuming you have two vectors containing floats with some NA, then:

C <- A*B

Will give you the products one at a time if the lengths are the same.
NA
times anything is NA.



Right - yes that works! - thanks!



Your second condition is also simple as you want anything below a
threshold
to be set to a fixes value.

Since you already have C, above, your condition of:

threshold <- 0.1
C < threshold

The last line returns a Boolean vector you can use to index C to get
just
the ones you select as TRUE and thus can change:

Result <- C[C < threshold]



Ah, I see . .



And you can of course do all the above as a one-liner.



Yes.



Is that what you wanted?



Exactly except I meant:

   Result <- C[C > threshold]

Thanks!

Phil.



-Original Message-
From: R-help  On Behalf Of Philip 
Rhoades

via
R-help
Sent: Tuesday, June 20, 2023 11:38 AM
To: r-help@r-project.org
Subject: [R] Multiplying two vectors of the same size to give a third
vector
of the same size

People,

I am assuming that what I want to do is easier in R than say Ruby.

I want to do what the Subject says ie multiply the cells in the same
position of two vectors (A and B) to give a result in the same 
position

in a third vector (C) BUT:

- The values in the cells of A and B are floats between 0.0 and 1.0 or
NULL

- If there is a NULL in the multiplication, then the result in the 
cell

for C is also a NULL

- If there is a value less than (say) 0.01 in the multiplication, then
the result in the cell for C is 0.0

Any suggestions appreciated!

Phil.

--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

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


--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] Multiplying two vectors of the same size to give a third vector of the same size

2023-06-20 Thread avi.e.gross
I was rushing out Phil so let me amend what I wrote. As others noted, this is 
fairly beginner stuff. If you have more such questions, besides reading up, 
please consider sending questions to the Tutor mailing list where there is more 
patience. 

You wanted to change selected small values to 0.0.

So you do not want the code I supplied as illustration as  it removes small 
elements making a smaller  vector.

Assume this test:

A <- c(NA, 0.3, 0.6, NA, 0.9)
B <- c(NA, 0.2, 0.6, 0.9, 0.9)

C <- A * B
print(C)

The result at this point is:

[1]   NA 0.06 0.36   NA 0.81

As expected, anything with an NA in either vector will generate an NA in the 
componentwise multiplication. Note the second item at 0.06 is under your 
threshold of 0.1.

What you want is not this:

Result <- C[C < 0.1]
> Result
[1]   NA 0.06   NA

That threw away anything above the threshold.

What you want may be this:

C[C < 0.1] <- 0.0
print(C)

This returns 

[1]   NA 0.00 0.36   NA 0.81

Everything that is NA or at or above 0.1 is kept and anything below 0.1 is 
zeroed and kept.

Of course, if you do not want to keep the NA, that can trivially be removed:

C[!is.na(C)]
[1] 0.00 0.36 0.81





-Original Message-
From: Philip Rhoades  
Sent: Tuesday, June 20, 2023 1:04 PM
To: avi.e.gr...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] Multiplying two vectors of the same size to give a third 
vector of the same size

avi,


On 2023-06-21 01:55, avi.e.gr...@gmail.com wrote:
> Phil,
> 
> What have you tried. This seems straightforward enough.
> 
> Could you clarify what you mean by NULL?


I guess in R in would just be an empty cell? - ie NOT a zero.


> In R, it is common to use NA or a more specific version of it.


Ah yes, that would be it I think.


> So assuming you have two vectors containing floats with some NA, then:
> 
> C <- A*B
> 
> Will give you the products one at a time if the lengths are the same. 
> NA
> times anything is NA.


Right - yes that works! - thanks!


> Your second condition is also simple as you want anything below a 
> threshold
> to be set to a fixes value.
> 
> Since you already have C, above, your condition of:
> 
> threshold <- 0.1
> C < threshold
> 
> The last line returns a Boolean vector you can use to index C to get 
> just
> the ones you select as TRUE and thus can change:
> 
> Result <- C[C < threshold]


Ah, I see . .


> And you can of course do all the above as a one-liner.


Yes.


> Is that what you wanted?


Exactly except I meant:

   Result <- C[C > threshold]

Thanks!

Phil.


> -Original Message-
> From: R-help  On Behalf Of Philip Rhoades 
> via
> R-help
> Sent: Tuesday, June 20, 2023 11:38 AM
> To: r-help@r-project.org
> Subject: [R] Multiplying two vectors of the same size to give a third 
> vector
> of the same size
> 
> People,
> 
> I am assuming that what I want to do is easier in R than say Ruby.
> 
> I want to do what the Subject says ie multiply the cells in the same
> position of two vectors (A and B) to give a result in the same position
> in a third vector (C) BUT:
> 
> - The values in the cells of A and B are floats between 0.0 and 1.0 or
> NULL
> 
> - If there is a NULL in the multiplication, then the result in the cell
> for C is also a NULL
> 
> - If there is a value less than (say) 0.01 in the multiplication, then
> the result in the cell for C is 0.0
> 
> Any suggestions appreciated!
> 
> Phil.
> 
> --
> Philip Rhoades
> 
> PO Box 896
> Cowra  NSW  2794
> Australia
> E-mail:  p...@pricom.com.au
> 
> __
> 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.

-- 
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] OK, next Q - a sort of factorial on a vector

2023-06-20 Thread Philip Rhoades via R-help

Eric,


On 2023-06-21 04:02, Eric Berger wrote:

Hi Philip,
In the decades since you learned R there have been some additions to
the language.
In particular, R now supports lambda functions.
Applying this feature to Ivan's beautiful solution cuts down 7
characters (continuing his golfing analogy)

unlist(lapply(seq_along(x), \(i) x[i] * x[-(1:i)]))



Amazing! - it reminds me of the old days when there were competitions to 
write the smallest C programs . .




Enjoy your return to R!



Thanks! I think I was right to look at R first - it is exactly what I 
need I think - and the model I am thinking of shouldn't need any grunt 
that would require rewriting any functions in C or Rust or something . .


P.



On Tue, Jun 20, 2023 at 8:46 PM Philip Rhoades via R-help
 wrote:


Ivan,

On 2023-06-21 03:32, Ivan Krylov wrote:

В Wed, 21 Jun 2023 03:13:52 +1000
Philip Rhoades via R-help  пишет:


This:

!(1,2,3,4,5)

would give this:

(2,3,4,5, 6,8,10, 12,15, 20)


Do you mean taking a product of every element of the vector with

all

following vector elements? A relatively straightforward way would

be

(given your vector stored in `x`):

unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)]))


Perfect!


(I'm sure it could be golfed further.)


I will look at Sarah's suggestion too.


and this:

!(1,2,NA,4,5)

would give this:

(2,4,5, 8,10, 20)


The previous solution seems to give your vector interspersed a

bunch of

NAs, so one way to continue would be to filter it using v[!is.na

[1](v)].

Exactly!

Thanks people - it would have taken forever to work that out myself
(it
has been decades since I looked at R).

Phil.
--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

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



Links:
--
[1] http://is.na


--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] OK, next Q - a sort of factorial on a vector

2023-06-20 Thread Eric Berger
Hi Philip,
In the decades since you learned R there have been some additions to the
language.
In particular, R now supports lambda functions.
Applying this feature to Ivan's beautiful solution cuts down 7 characters
(continuing his golfing analogy)

unlist(lapply(seq_along(x), \(i) x[i] * x[-(1:i)]))

Enjoy your return to R!


On Tue, Jun 20, 2023 at 8:46 PM Philip Rhoades via R-help <
r-help@r-project.org> wrote:

> Ivan,
>
>
> On 2023-06-21 03:32, Ivan Krylov wrote:
> > В Wed, 21 Jun 2023 03:13:52 +1000
> > Philip Rhoades via R-help  пишет:
> >
> >> This:
> >>
> >>!(1,2,3,4,5)
> >>
> >> would give this:
> >>
> >>(2,3,4,5, 6,8,10, 12,15, 20)
> >
> > Do you mean taking a product of every element of the vector with all
> > following vector elements? A relatively straightforward way would be
> > (given your vector stored in `x`):
> >
> > unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)]))
>
>
> Perfect!
>
>
> > (I'm sure it could be golfed further.)
>
>
> I will look at Sarah's suggestion too.
>
>
> >> and this:
> >>
> >>!(1,2,NA,4,5)
> >>
> >> would give this:
> >>
> >>(2,4,5, 8,10, 20)
> >
> > The previous solution seems to give your vector interspersed a bunch of
> > NAs, so one way to continue would be to filter it using v[!is.na(v)].
>
>
> Exactly!
>
> Thanks people - it would have taken forever to work that out myself (it
> has been decades since I looked at R).
>
> Phil.
> --
> Philip Rhoades
>
> PO Box 896
> Cowra  NSW  2794
> Australia
> E-mail:  p...@pricom.com.au
>
> __
> 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] Multiplying two vectors of the same size to give a third vector of the same size

2023-06-20 Thread Philip Rhoades via R-help

Bert,


On 2023-06-21 03:31, Bert Gunter wrote:

IMO:
You need to do your due diligence. This list is not meant to serve as
a substitute for learning the basics of R, which you have clearly not
done. Please go through an R tutorial or two (there is even one that
ships with R, "An Inroduction to R") before posting further here.
There are many good ones online, of course. Here is one compendium,
but a simple search will find many more:

https://support.posit.co/hc/en-us/articles/200552336-Getting-Help-with-R



It has been a long time since I looked at R and I thought at least my 
second Q was something reasonable to ask the list?


Anyway now that I need to use R again, the basics should come back to me 
I hope - but I will check out the tutorials again in any case . .


Thanks to Uwe as well (very nice!),

Phil.

On Tue, Jun 20, 2023 at 8:38 AM Philip Rhoades via R-help
 wrote:


People,

I am assuming that what I want to do is easier in R than say Ruby.

I want to do what the Subject says ie multiply the cells in the same

position of two vectors (A and B) to give a result in the same
position
in a third vector (C) BUT:

- The values in the cells of A and B are floats between 0.0 and 1.0
or
NULL

- If there is a NULL in the multiplication, then the result in the
cell
for C is also a NULL

- If there is a value less than (say) 0.01 in the multiplication,
then
the result in the cell for C is 0.0

Any suggestions appreciated!

Phil.

--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

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


--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] OK, next Q - a sort of factorial on a vector

2023-06-20 Thread Philip Rhoades via R-help

Ivan,


On 2023-06-21 03:32, Ivan Krylov wrote:

В Wed, 21 Jun 2023 03:13:52 +1000
Philip Rhoades via R-help  пишет:


This:

   !(1,2,3,4,5)

would give this:

   (2,3,4,5, 6,8,10, 12,15, 20)


Do you mean taking a product of every element of the vector with all
following vector elements? A relatively straightforward way would be
(given your vector stored in `x`):

unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)]))



Perfect!



(I'm sure it could be golfed further.)



I will look at Sarah's suggestion too.



and this:

   !(1,2,NA,4,5)

would give this:

   (2,4,5, 8,10, 20)


The previous solution seems to give your vector interspersed a bunch of
NAs, so one way to continue would be to filter it using v[!is.na(v)].



Exactly!

Thanks people - it would have taken forever to work that out myself (it 
has been decades since I looked at R).


Phil.
--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] OK, next Q - a sort of factorial on a vector

2023-06-20 Thread Uwe Ligges

vf <- function(x){
  o <- outer(x, x)
  as.vector(na.omit(o[lower.tri(o)]))
}

vf(1:5)
vf(c(1,2,NA,4,5))


Best,
Uwe Ligges



On 20.06.2023 19:13, Philip Rhoades via R-help wrote:

People,

What I mean is, is there an elegant way to do this:

This:

   !(1,2,3,4,5)

would give this:

   (2,3,4,5, 6,8,10, 12,15, 20)

and this:

   !(1,2,NA,4,5)

would give this:

   (2,4,5, 8,10, 20)

?

Thanks!

Phil.


__
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] OK, next Q - a sort of factorial on a vector

2023-06-20 Thread Sarah Goslee
Well, I think this is reasonable elegant, but ymmv. Turning it into a
function and removing NA values is left for you.

> x <- 1:5
> unlist(sapply(seq(1, length(x) - 1), function(i){x[i] * x[seq(i + 1, 
> length(x))]}))
 [1]  2  3  4  5  6  8 10 12 15 20
>
> x <- c(1, 2, NA, 4, 5)
> unlist(sapply(seq(1, length(x) - 1), function(i){x[i] * x[seq(i + 1, 
> length(x))]}))
 [1]  2 NA  4  5 NA  8 10 NA NA 20

Sarah

On Tue, Jun 20, 2023 at 1:15 PM Philip Rhoades via R-help
 wrote:
>
> People,
>
> What I mean is, is there an elegant way to do this:
>
> This:
>
>!(1,2,3,4,5)
>
> would give this:
>
>(2,3,4,5, 6,8,10, 12,15, 20)
>
> and this:
>
>!(1,2,NA,4,5)
>
> would give this:
>
>(2,4,5, 8,10, 20)
>
> ?
>
> Thanks!
>
> Phil.
> --
> Philip Rhoades
>
> PO Box 896
> Cowra  NSW  2794
> Australia
> E-mail:  p...@pricom.com.au
>
> __
> 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.



-- 
Sarah Goslee (she/her)
http://www.numberwright.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] OK, next Q - a sort of factorial on a vector

2023-06-20 Thread Ivan Krylov
В Wed, 21 Jun 2023 03:13:52 +1000
Philip Rhoades via R-help  пишет:

> This:
> 
>!(1,2,3,4,5)
> 
> would give this:
> 
>(2,3,4,5, 6,8,10, 12,15, 20)

Do you mean taking a product of every element of the vector with all
following vector elements? A relatively straightforward way would be
(given your vector stored in `x`):

unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)]))

(I'm sure it could be golfed further.)

> and this:
> 
>!(1,2,NA,4,5)
> 
> would give this:
> 
>(2,4,5, 8,10, 20)

The previous solution seems to give your vector interspersed a bunch of
NAs, so one way to continue would be to filter it using v[!is.na(v)].

-- 
Best regards,
Ivan

__
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] Multiplying two vectors of the same size to give a third vector of the same size

2023-06-20 Thread Bert Gunter
IMO:
You need to do your due diligence. This list is not meant to serve as a
substitute for learning the basics of R, which you have clearly not done.
Please go through an R tutorial or two (there is even one that ships with
R, "An Inroduction to R") before posting further here. There are many good
ones online, of course. Here is one compendium, but a simple search will
find many more:
https://support.posit.co/hc/en-us/articles/200552336-Getting-Help-with-R

Cheers,
Bert
 even on

On Tue, Jun 20, 2023 at 8:38 AM Philip Rhoades via R-help <
r-help@r-project.org> wrote:

> People,
>
> I am assuming that what I want to do is easier in R than say Ruby.
>
> I want to do what the Subject says ie multiply the cells in the same
> position of two vectors (A and B) to give a result in the same position
> in a third vector (C) BUT:
>
> - The values in the cells of A and B are floats between 0.0 and 1.0 or
> NULL
>
> - If there is a NULL in the multiplication, then the result in the cell
> for C is also a NULL
>
> - If there is a value less than (say) 0.01 in the multiplication, then
> the result in the cell for C is 0.0
>
> Any suggestions appreciated!
>
> Phil.
>
> --
> Philip Rhoades
>
> PO Box 896
> Cowra  NSW  2794
> Australia
> E-mail:  p...@pricom.com.au
>
> __
> 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] OK, next Q - a sort of factorial on a vector

2023-06-20 Thread Philip Rhoades via R-help

People,

What I mean is, is there an elegant way to do this:

This:

  !(1,2,3,4,5)

would give this:

  (2,3,4,5, 6,8,10, 12,15, 20)

and this:

  !(1,2,NA,4,5)

would give this:

  (2,4,5, 8,10, 20)

?

Thanks!

Phil.
--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] Multiplying two vectors of the same size to give a third vector of the same size

2023-06-20 Thread Philip Rhoades via R-help

avi,


On 2023-06-21 01:55, avi.e.gr...@gmail.com wrote:

Phil,

What have you tried. This seems straightforward enough.

Could you clarify what you mean by NULL?



I guess in R in would just be an empty cell? - ie NOT a zero.



In R, it is common to use NA or a more specific version of it.



Ah yes, that would be it I think.



So assuming you have two vectors containing floats with some NA, then:

C <- A*B

Will give you the products one at a time if the lengths are the same. 
NA

times anything is NA.



Right - yes that works! - thanks!


Your second condition is also simple as you want anything below a 
threshold

to be set to a fixes value.

Since you already have C, above, your condition of:

threshold <- 0.1
C < threshold

The last line returns a Boolean vector you can use to index C to get 
just

the ones you select as TRUE and thus can change:

Result <- C[C < threshold]



Ah, I see . .



And you can of course do all the above as a one-liner.



Yes.



Is that what you wanted?



Exactly except I meant:

  Result <- C[C > threshold]

Thanks!

Phil.



-Original Message-
From: R-help  On Behalf Of Philip Rhoades 
via

R-help
Sent: Tuesday, June 20, 2023 11:38 AM
To: r-help@r-project.org
Subject: [R] Multiplying two vectors of the same size to give a third 
vector

of the same size

People,

I am assuming that what I want to do is easier in R than say Ruby.

I want to do what the Subject says ie multiply the cells in the same
position of two vectors (A and B) to give a result in the same position
in a third vector (C) BUT:

- The values in the cells of A and B are floats between 0.0 and 1.0 or
NULL

- If there is a NULL in the multiplication, then the result in the cell
for C is also a NULL

- If there is a value less than (say) 0.01 in the multiplication, then
the result in the cell for C is 0.0

Any suggestions appreciated!

Phil.

--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

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


--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] Multiplying two vectors of the same size to give a third vector of the same size

2023-06-20 Thread avi.e.gross
Phil,

What have you tried. This seems straightforward enough.

Could you clarify what you mean by NULL?

In R, it is common to use NA or a more specific version of it.

So assuming you have two vectors containing floats with some NA, then:

C <- A*B

Will give you the products one at a time if the lengths are the same. NA
times anything is NA.

Your second condition is also simple as you want anything below a threshold
to be set to a fixes value.

Since you already have C, above, your condition of:

threshold <- 0.1
C < threshold

The last line returns a Boolean vector you can use to index C to get just
the ones you select as TRUE and thus can change:

Result <- C[C < threshold]

And you can of course do all the above as a one-liner.

Is that what you wanted?


-Original Message-
From: R-help  On Behalf Of Philip Rhoades via
R-help
Sent: Tuesday, June 20, 2023 11:38 AM
To: r-help@r-project.org
Subject: [R] Multiplying two vectors of the same size to give a third vector
of the same size

People,

I am assuming that what I want to do is easier in R than say Ruby.

I want to do what the Subject says ie multiply the cells in the same 
position of two vectors (A and B) to give a result in the same position 
in a third vector (C) BUT:

- The values in the cells of A and B are floats between 0.0 and 1.0 or 
NULL

- If there is a NULL in the multiplication, then the result in the cell 
for C is also a NULL

- If there is a value less than (say) 0.01 in the multiplication, then 
the result in the cell for C is 0.0

Any suggestions appreciated!

Phil.

-- 
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] Multiplying two vectors of the same size to give a third vector of the same size

2023-06-20 Thread Philip Rhoades via R-help

People,

I am assuming that what I want to do is easier in R than say Ruby.

I want to do what the Subject says ie multiply the cells in the same 
position of two vectors (A and B) to give a result in the same position 
in a third vector (C) BUT:


- The values in the cells of A and B are floats between 0.0 and 1.0 or 
NULL


- If there is a NULL in the multiplication, then the result in the cell 
for C is also a NULL


- If there is a value less than (say) 0.01 in the multiplication, then 
the result in the cell for C is 0.0


Any suggestions appreciated!

Phil.

--
Philip Rhoades

PO Box 896
Cowra  NSW  2794
Australia
E-mail:  p...@pricom.com.au

__
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] inconsistency in mclapply.....

2023-06-20 Thread akshay kulkarni
Dear Ivan,
 Yes, you were rightthe native BLAS version of my ubuntu 
system is:

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

Updating BLAS with ropenblas(), as you said,is superficial.

Will reverting back to you if stuck againwith mclapply..

A million thanks  again...

THanking you,
Yours sincerely,
AKSHAY M KULKKARNI

From: Ivan Krylov 
Sent: Sunday, June 18, 2023 12:42 AM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] inconsistency in mclapply.

� Sat, 17 Jun 2023 18:54:43 +
akshay kulkarni  �:

>   1.  I was working on a two core machine just to test the
> code...tomorrow will be switching to a 48 core machine. Should I
> again download the latest openblas from ropenblas() or do you suggest
> testing the native openblas that is bundled in Ubuntu

What's the version of OpenBLAS in the Ubuntu release you are running?
Unless you can see significant improvements regarding your CPU
architecture appearing since the version included in Ubuntu was
released, there's little reason to install a different version
manually. The whole point of a GNU/Linux distro is not having to
compile everything yourself. Ubuntu's build of OpenBLAS contains
multiple copies of the code targeted at different processors, including
yours.

> 2.  How do you pass the version of openblas to be installed with
> ropenblas()? The full file name? or just something like this:
> libopenblas_skylakexp-r0.3.23.so  ?

It looks like this function accepts the names of git tags in the
official OpenBLAS Git repository, but it's really the job of the
package maintainer to document important arguments like this one
properly. You can call bug.report(package = 'ropenblas') or write an
e-mail to maintainer('ropenblas') or locate the author's GitHub and ask
a question there.

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