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 <p...@pricom.com.au>
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 <r-help-boun...@r-project.org> 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.