Philip Rhoades
2023-Jun-20 15:38 UTC
[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: phil at pricom.com.au
@vi@e@gross m@iii@g oii gm@ii@com
2023-Jun-20 15:55 UTC
[R] Multiplying two vectors of the same size to give a third vector of the same size
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 <r-help-bounces at r-project.org> On Behalf Of Philip Rhoades via R-help Sent: Tuesday, June 20, 2023 11:38 AM To: r-help at 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: phil at pricom.com.au ______________________________________________ R-help at 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.
Bert Gunter
2023-Jun-20 17:31 UTC
[R] Multiplying two vectors of the same size to give a third vector of the same size
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 at 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: phil at pricom.com.au > > ______________________________________________ > R-help at 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]]