replace zero in a matrix using random numbers

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

replace zero in a matrix using random numbers

Yuan Chun Ding
Hi R users,

I am analyzing miRNA sequence data for a special network analysis, I need to replace zero values in a matrix as random numbers with mean of 1 and standard deviation of 0.1.

er.miRCounts[er.miRCounts==0] <- rnorm(1,mean=1, sd=0.1);

this code made all zero values as 1.13, not random numbers across different zero values.  If all zero values in one row are replaced by the same 1.13,  then sd in that row is zero, causing other problem in the following calculation.

Can you help me?

Thank you,

Yuan chun Ding

----------------------------------------------------------------------
------------------------------------------------------------
-SECURITY/CONFIDENTIALITY WARNING-  

This message and any attachments are intended solely for the individual or entity to which they are addressed. This communication may contain information that is privileged, confidential, or exempt from disclosure under applicable law (e.g., personal health information, research data, financial information). Because this e-mail has been sent without encryption, individuals other than the intended recipient may be able to view the information, forward it to others or tamper with the information without the knowledge or consent of the sender. If you are not the intended recipient, or the employee or person responsible for delivering the message to the intended recipient, any dissemination, distribution or copying of the communication is strictly prohibited. If you received the communication in error, please notify the sender immediately by replying to this message and deleting the message and any accompanying files from your system. If, due to the security risks, you do not wish to receive further communications via e-mail, please reply to this message and inform the sender that you do not wish to receive further e-mail from the sender. (LCP301)

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: replace zero in a matrix using random numbers

Rui Barradas
Hello,

Try to get the number of zero values before:

i <- er.miRCounts == 0
n <- sum(i)
er.miRCounts[i] <- rnorm(n,mean=1, sd=0.1)


Also, there is no need for a end-of-instruction ';'.
The semi-colon is the instruction separator, so if you put it at the end
you will be separating the instruction you wrote from the NULL
instruction. A simpler example could be

mean(1:5);

This is *two* instructions:

mean(1:5); NULL


Hope this helps,

Rui Barradas

Às 22:33 de 08/01/21, Yuan Chun Ding escreveu:

> Hi R users,
>
> I am analyzing miRNA sequence data for a special network analysis, I need to replace zero values in a matrix as random numbers with mean of 1 and standard deviation of 0.1.
>
> er.miRCounts[er.miRCounts==0] <- rnorm(1,mean=1, sd=0.1);
>
> this code made all zero values as 1.13, not random numbers across different zero values.  If all zero values in one row are replaced by the same 1.13,  then sd in that row is zero, causing other problem in the following calculation.
>
> Can you help me?
>
> Thank you,
>
> Yuan chun Ding
>
> ----------------------------------------------------------------------
> ------------------------------------------------------------
> -SECURITY/CONFIDENTIALITY WARNING-
>
> This message and any attachments are intended solely for the individual or entity to which they are addressed. This communication may contain information that is privileged, confidential, or exempt from disclosure under applicable law (e.g., personal health information, research data, financial information). Because this e-mail has been sent without encryption, individuals other than the intended recipient may be able to view the information, forward it to others or tamper with the information without the knowledge or consent of the sender. If you are not the intended recipient, or the employee or person responsible for delivering the message to the intended recipient, any dissemination, distribution or copying of the communication is strictly prohibited. If you received the communication in error, please notify the sender immediately by replying to this message and deleting the message and any accompanying files from your system. If, due to the security risks, you do not wish to receive further communications via e-mail, please reply to this message and inform the sender that you do not wish to receive further e-mail from the sender. (LCP301)
>
> ______________________________________________
> [hidden email] 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.
>
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: replace zero in a matrix using random numbers

Bert Gunter-2
In reply to this post by Yuan Chun Ding
?rnorm tells you that n, the first argument, is the number of
observations/random numbers you wish to generate. You asked for 1.
So you need to ask for the number of 0's, something like:

> a <- matrix(rep(0:1, 3), nrow =3)
> a
     [,1] [,2]
[1,]    0    1
[2,]    1    0
[3,]    0    1
> a[!a] <- rnorm(sum(!a), 1,.1)
> a
          [,1]     [,2]
[1,] 0.8136788 1.000000
[2,] 1.0000000 1.146225
[3,] 0.9081908 1.000000

Bert


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 Fri, Jan 8, 2021 at 2:33 PM Yuan Chun Ding <[hidden email]> wrote:

> Hi R users,
>
> I am analyzing miRNA sequence data for a special network analysis, I need
> to replace zero values in a matrix as random numbers with mean of 1 and
> standard deviation of 0.1.
>
> er.miRCounts[er.miRCounts==0] <- rnorm(1,mean=1, sd=0.1);
>
> this code made all zero values as 1.13, not random numbers across
> different zero values.  If all zero values in one row are replaced by the
> same 1.13,  then sd in that row is zero, causing other problem in the
> following calculation.
>
> Can you help me?
>
> Thank you,
>
> Yuan chun Ding
>
> ----------------------------------------------------------------------
> ------------------------------------------------------------
> -SECURITY/CONFIDENTIALITY WARNING-
>
> This message and any attachments are intended solely for the individual or
> entity to which they are addressed. This communication may contain
> information that is privileged, confidential, or exempt from disclosure
> under applicable law (e.g., personal health information, research data,
> financial information). Because this e-mail has been sent without
> encryption, individuals other than the intended recipient may be able to
> view the information, forward it to others or tamper with the information
> without the knowledge or consent of the sender. If you are not the intended
> recipient, or the employee or person responsible for delivering the message
> to the intended recipient, any dissemination, distribution or copying of
> the communication is strictly prohibited. If you received the communication
> in error, please notify the sender immediately by replying to this message
> and deleting the message and any accompanying files from your system. If,
> due to the security risks, you do not wish to receive further
> communications via e-mail, please reply to this message and inform the
> sender that you do not wish to receive further e-mail from the sender.
> (LCP301)
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: replace zero in a matrix using random numbers

Yuan Chun Ding
Hi Bert and Rui,

Thank you very much!  I had thought for every condition of  matrix ==0 ,  rnrom(n=1, 1, 0.1) will randomly generate a number with mean 1 and sd 0.1.

Yuan

From: Bert Gunter [mailto:[hidden email]]
Sent: Friday, January 8, 2021 2:49 PM
To: Yuan Chun Ding <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] replace zero in a matrix using random numbers

?rnorm tells you that n, the first argument, is the number of observations/random numbers you wish to generate. You asked for 1.
So you need to ask for the number of 0's, something like:

> a <- matrix(rep(0:1, 3), nrow =3)
> a
     [,1] [,2]
[1,]    0    1
[2,]    1    0
[3,]    0    1
> a[!a] <- rnorm(sum(!a), 1,.1)
> a
          [,1]     [,2]
[1,] 0.8136788 1.000000
[2,] 1.0000000 1.146225
[3,] 0.9081908 1.000000

Bert


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 Fri, Jan 8, 2021 at 2:33 PM Yuan Chun Ding <[hidden email]<mailto:[hidden email]>> wrote:
Hi R users,

I am analyzing miRNA sequence data for a special network analysis, I need to replace zero values in a matrix as random numbers with mean of 1 and standard deviation of 0.1.

er.miRCounts[er.miRCounts==0] <- rnorm(1,mean=1, sd=0.1);

this code made all zero values as 1.13, not random numbers across different zero values.  If all zero values in one row are replaced by the same 1.13,  then sd in that row is zero, causing other problem in the following calculation.

Can you help me?

Thank you,

Yuan chun Ding

----------------------------------------------------------------------
------------------------------------------------------------
-SECURITY/CONFIDENTIALITY WARNING-

This message and any attachments are intended solely for the individual or entity to which they are addressed. This communication may contain information that is privileged, confidential, or exempt from disclosure under applicable law (e.g., personal health information, research data, financial information). Because this e-mail has been sent without encryption, individuals other than the intended recipient may be able to view the information, forward it to others or tamper with the information without the knowledge or consent of the sender. If you are not the intended recipient, or the employee or person responsible for delivering the message to the intended recipient, any dissemination, distribution or copying of the communication is strictly prohibited. If you received the communication in error, please notify the sender immediately by replying to this message and deleting the message and any accompanying files from your system. If, due to the security risks, you do not wish to rec
 eive further communications via e-mail, please reply to this message and inform the sender that you do not wish to receive further e-mail from the sender. (LCP301)

______________________________________________
[hidden email]<mailto:[hidden email]> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help<https://urldefense.com/v3/__https:/stat.ethz.ch/mailman/listinfo/r-help__;!!Fou38LsQmgU!6DmiKt7lXx7yfq0jUsdRHh-P5ZiKxoViTNaEApo8IZkGCfXY5v2vD0NYpbXt$>
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<https://urldefense.com/v3/__http:/www.R-project.org/posting-guide.html__;!!Fou38LsQmgU!6DmiKt7lXx7yfq0jUsdRHh-P5ZiKxoViTNaEApo8IZkGCfXY5v2vD0eS8lpe$>
and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.