Re: [R] replace zero in a matrix using random numbers

2021-01-08 Thread 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:bgunter.4...@gmail.com]
Sent: Friday, January 8, 2021 2:49 PM
To: Yuan Chun Ding 
Cc: r-help@r-project.org
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,]01
[2,]10
[3,]01
> a[!a] <- rnorm(sum(!a), 1,.1)
> a
  [,1] [,2]
[1,] 0.8136788 1.00
[2,] 1.000 1.146225
[3,] 0.9081908 1.00

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 
mailto:ycd...@coh.org>> 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)

__
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] replace zero in a matrix using random numbers

2021-01-08 Thread Bert Gunter
?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,]01
[2,]10
[3,]01
> a[!a] <- rnorm(sum(!a), 1,.1)
> a
  [,1] [,2]
[1,] 0.8136788 1.00
[2,] 1.000 1.146225
[3,] 0.9081908 1.00

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  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)
>
> __
> 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] replace zero in a matrix using random numbers

2021-01-08 Thread 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)

__
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] replace zero in a matrix using random numbers

2021-01-08 Thread 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)

__
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] HW help

2021-01-08 Thread JRG via R-help
This list has a no-homework policy.

---JRG




On 2021-01-08 09:43, zyra e madhe wrote:
> Hello,
> 
> I'm having difficulty doing the following exercise.
> Can you please provide the code and some written explanation?
> 
> Thank you in advance,
> 
> Zyra
> 
> 
> __
> 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] HW help

2021-01-08 Thread zyra e madhe
Hello,

I'm having difficulty doing the following exercise.
Can you please provide the code and some written explanation?

Thank you in advance,

Zyra
__
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] Secondary y axis in ggplot2: did not respond when change its y-axis value

2021-01-08 Thread Marna Wagley
Thank you Rui and Thierry for the suggestion, it helped me.
thanks


On Fri, Jan 8, 2021 at 6:58 AM Rui Barradas  wrote:

> Hello,
>
> What about the following?
> First get the min and max of value by variable == "y1".
> Then use that range to scale up "y2".
>
> rng <- tapply(daT1$value, daT1$variable, range)$y1
>
> ggplot(data = daT1, aes(x = x, group = variable, color = variable)) +
>geom_line(data = subset(daT1, variable == "y1"),
>  aes(y = value)) +
>geom_line(data = subset(daT1, variable == "y2"),
>  aes(y = value*diff(rng) + rng[1])) +
>facet_wrap(~group) +
>scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 01:01 de 08/01/21, Marna Wagley escreveu:
> > Hi R users,
> > I was trying to plot a graph with a secondary axis, and used the
> following
> > code for the data but the secondary line and secondary y -axis value did
> > not match. I would like to show both lines in one graph.
> >
> > Any suggestions?
> >
> > library(ggplot2)
> > library(reshape2)
> > daT<-structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
> > 3L, 4L, 5L, 6L, 7L, 8L), y1 = c(9754L, 1051L, 5833L, 5769L, 2479L,
> > 470L, 5828L, 174L, 2045L, 6099L, 8780L, 8732L, 4053L, 9419L,
> > 4728L, 3587L), y2 = c(0.51, 0.61, 0.3, 0.81, 0.89, 0, 1.9, 0.76,
> > 0.87, 0.29, 0, 0.42, 0.73, 0.96, 0.62, 0.06), group = c("A",
> > "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B",
> > "B", "B")), class = "data.frame", row.names = c(NA, -16L))
> > print(daT)
> > daT1<-melt(daT, id.vars=c("x", "group"))
> > daT1%>%
> >ggplot() +
> >geom_line(aes(x = x, y = value, group = variable, color = variable)) +
> >facet_wrap(~group) +
> >scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))
> >
> >   [[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.
> >
>

[[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] Secondary y axis in ggplot2: did not respond when change its y-axis value

2021-01-08 Thread Rui Barradas

Hello,

What about the following?
First get the min and max of value by variable == "y1".
Then use that range to scale up "y2".

rng <- tapply(daT1$value, daT1$variable, range)$y1

ggplot(data = daT1, aes(x = x, group = variable, color = variable)) +
  geom_line(data = subset(daT1, variable == "y1"),
aes(y = value)) +
  geom_line(data = subset(daT1, variable == "y2"),
aes(y = value*diff(rng) + rng[1])) +
  facet_wrap(~group) +
  scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))


Hope this helps,

Rui Barradas

Às 01:01 de 08/01/21, Marna Wagley escreveu:

Hi R users,
I was trying to plot a graph with a secondary axis, and used the following
code for the data but the secondary line and secondary y -axis value did
not match. I would like to show both lines in one graph.

Any suggestions?

library(ggplot2)
library(reshape2)
daT<-structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L), y1 = c(9754L, 1051L, 5833L, 5769L, 2479L,
470L, 5828L, 174L, 2045L, 6099L, 8780L, 8732L, 4053L, 9419L,
4728L, 3587L), y2 = c(0.51, 0.61, 0.3, 0.81, 0.89, 0, 1.9, 0.76,
0.87, 0.29, 0, 0.42, 0.73, 0.96, 0.62, 0.06), group = c("A",
"A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B",
"B", "B")), class = "data.frame", row.names = c(NA, -16L))
print(daT)
daT1<-melt(daT, id.vars=c("x", "group"))
daT1%>%
   ggplot() +
   geom_line(aes(x = x, y = value, group = variable, color = variable)) +
   facet_wrap(~group) +
   scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))

[[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-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] non-standard reshape from long to wide

2021-01-08 Thread PIKAL Petr
Hi

dcast from reshape is close, however column order is different

mydf <- dcast(df.long, sample~marker)
(!is.na(mydf[,-1]))*1
 g j k u x y
[1,] 1 0 1 0 1 0
[2,] 0 1 0 1 1 1
[3,] 0 0 0 0 1 1

You just need to change 0 to NA and add rownames from mydf.

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Yuan Chun Ding
> Sent: Thursday, January 7, 2021 7:40 PM
> To: r-help@r-project.org
> Subject: [R] non-standard reshape from long to wide
> 
> Dear R user,
> 
> I want to reshape a long data frame to wide format, I made the following
> example files.  Can you help me?
> 
> Thank you,
> 
> Yuan Chun Ding
> 
> sample <-c("xr" , "xr" , "fh" , "fh" , "fh" , "uy" , "uy" , "uy" , "uy");
marker <-
> c("x" , "y" , "g" , "x" , "k" , "y" , "x" , "u" , "j"); df.long
<-data.frame(sample,
> marker);
> 
> xr <-c(1,1,NA,NA,NA,NA);
> fh <-c(1,NA,1,1,NA,NA);
> uy <-c(1,1,NA,NA,1,1);
> 
> df.wide <- t(data.frame(xr,fh,uy));
> colnames(df.wide)<-c("x","y","g","k", "u","j");
> 
> --
> 
> -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)
> 
> __
> 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.