

Hello,
I have a data frame which looks like this:
> head(pt)
eidQ phenoQ phenoH
1 1000017 9 9
2 1000025 9 9
3 1000038 9 1
4 1000042 9 9
5 1000056 9 9
6 1000074 9 9
7 1000038 9 1
8 1000127 2 1
9 1000690 2 9
10 1000711 2 9
11 1001431 2 1
12 1001710 9 1
I would like to create the 3rd column called "pheno" which would have
these values:
9,9,1,9,9,9,1,2,2,2,2,1
so in other words:
if 9 appears in both phenoQ and phenoH I will have 9 in pheno
if 2 appears in any of phenoQ or phenoH I will have 2 in pheno
if I have 9 and 1 or 1 and 9 in those columns I will have 1 in pheno
if I have 9 and 2 or 2 and 9 in those columns I will have 2 in pheno
Can you please tell me how my if else statement would look like or any
other way how to do that in R if my data frame name is "pt"
Thanks
Ana
Ana
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


does this look ok:
pt$pheno=ifelse(pt$phenoQ==9 & pt$phenoH==9,9,ifelse(pt$phenoH==2 
pt$phenoQ==2,2,1))
On Wed, Aug 7, 2019 at 1:40 PM Ana Marija < [hidden email]> wrote:
>
> Hello,
>
> I have a data frame which looks like this:
>
> > head(pt)
> eidQ phenoQ phenoH
> 1 1000017 9 9
> 2 1000025 9 9
> 3 1000038 9 1
> 4 1000042 9 9
> 5 1000056 9 9
> 6 1000074 9 9
> 7 1000038 9 1
> 8 1000127 2 1
> 9 1000690 2 9
> 10 1000711 2 9
> 11 1001431 2 1
> 12 1001710 9 1
>
> I would like to create the 3rd column called "pheno" which would have
> these values:
> 9,9,1,9,9,9,1,2,2,2,2,1
>
> so in other words:
> if 9 appears in both phenoQ and phenoH I will have 9 in pheno
> if 2 appears in any of phenoQ or phenoH I will have 2 in pheno
> if I have 9 and 1 or 1 and 9 in those columns I will have 1 in pheno
> if I have 9 and 2 or 2 and 9 in those columns I will have 2 in pheno
>
> Can you please tell me how my if else statement would look like or any
> other way how to do that in R if my data frame name is "pt"
>
> Thanks
> Ana
> Ana
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


pmax() should work in this instance, as in any case you want the larger value.
Andrew

Andrew Robinson
Director, CEBRA, School of BioSciences
Reader & Associate Professor in Applied Statistics Tel: (+61) 0403 138 955
School of Mathematics and Statistics Fax: (+61) 03 8344 4599
University of Melbourne, VIC 3010 Australia
Email: [hidden email]
Website: http://cebra.unimelb.edu.au/________________________________
From: Rhelp < [hidden email]> on behalf of Ana Marija < [hidden email]>
Sent: Thursday, August 8, 2019 4:37 am
To: rhelp
Subject: [R] Help with if else statement
Hello,
I have a data frame which looks like this:
> head(pt)
eidQ phenoQ phenoH
1 1000017 9 9
2 1000025 9 9
3 1000038 9 1
4 1000042 9 9
5 1000056 9 9
6 1000074 9 9
7 1000038 9 1
8 1000127 2 1
9 1000690 2 9
10 1000711 2 9
11 1001431 2 1
12 1001710 9 1
I would like to create the 3rd column called "pheno" which would have
these values:
9,9,1,9,9,9,1,2,2,2,2,1
so in other words:
if 9 appears in both phenoQ and phenoH I will have 9 in pheno
if 2 appears in any of phenoQ or phenoH I will have 2 in pheno
if I have 9 and 1 or 1 and 9 in those columns I will have 1 in pheno
if I have 9 and 2 or 2 and 9 in those columns I will have 2 in pheno
Can you please tell me how my if else statement would look like or any
other way how to do that in R if my data frame name is "pt"
Thanks
Ana
Ana
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


The ifelse() construction is fast, but after a couple of nested iterations,
it gets messy and errorprone; so I believe to be avoided.
In your case, there is a much better alternative, ?pmax . Ergo, something
like:
pt$pheno < do.call(pmax, pt[, 1])
?do.call is necessary to pass the list of columns pt[, 1] to pmax.
Cheers,
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 Wed, Aug 7, 2019 at 11:55 AM Ana Marija < [hidden email]>
wrote:
> does this look ok:
>
> pt$pheno=ifelse(pt$phenoQ==9 & pt$phenoH==9,9,ifelse(pt$phenoH==2 
> pt$phenoQ==2,2,1))
>
> On Wed, Aug 7, 2019 at 1:40 PM Ana Marija < [hidden email]>
> wrote:
> >
> > Hello,
> >
> > I have a data frame which looks like this:
> >
> > > head(pt)
> > eidQ phenoQ phenoH
> > 1 1000017 9 9
> > 2 1000025 9 9
> > 3 1000038 9 1
> > 4 1000042 9 9
> > 5 1000056 9 9
> > 6 1000074 9 9
> > 7 1000038 9 1
> > 8 1000127 2 1
> > 9 1000690 2 9
> > 10 1000711 2 9
> > 11 1001431 2 1
> > 12 1001710 9 1
> >
> > I would like to create the 3rd column called "pheno" which would have
> > these values:
> > 9,9,1,9,9,9,1,2,2,2,2,1
> >
> > so in other words:
> > if 9 appears in both phenoQ and phenoH I will have 9 in pheno
> > if 2 appears in any of phenoQ or phenoH I will have 2 in pheno
> > if I have 9 and 1 or 1 and 9 in those columns I will have 1 in pheno
> > if I have 9 and 2 or 2 and 9 in those columns I will have 2 in pheno
> >
> > Can you please tell me how my if else statement would look like or any
> > other way how to do that in R if my data frame name is "pt"
> >
> > Thanks
> > Ana
> > Ana
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Hi Ana,
Or just for a bit of fun:
pt<read.table(text="eidQ phenoQ phenoH
1000017 9 9
1000025 9 9
1000038 9 1
1000042 9 9
1000056 9 9
1000074 9 9
1000038 9 1
1000127 2 1
1000690 2 9
1000711 2 9
1001431 2 1
1001710 9 1",
header=TRUE)
pt$pheno<apply(pt[,2:3],1,FUN=max)
Jim
On Thu, Aug 8, 2019 at 4:37 AM Ana Marija < [hidden email]> wrote:
>
> Hello,
>
> I have a data frame which looks like this:
>
> > head(pt)
> eidQ phenoQ phenoH
> 1 1000017 9 9
> 2 1000025 9 9
> 3 1000038 9 1
> 4 1000042 9 9
> 5 1000056 9 9
> 6 1000074 9 9
> 7 1000038 9 1
> 8 1000127 2 1
> 9 1000690 2 9
> 10 1000711 2 9
> 11 1001431 2 1
> 12 1001710 9 1
>
> I would like to create the 3rd column called "pheno" which would have
> these values:
> 9,9,1,9,9,9,1,2,2,2,2,1
>
> so in other words:
> if 9 appears in both phenoQ and phenoH I will have 9 in pheno
> if 2 appears in any of phenoQ or phenoH I will have 2 in pheno
> if I have 9 and 1 or 1 and 9 in those columns I will have 1 in pheno
> if I have 9 and 2 or 2 and 9 in those columns I will have 2 in pheno
>
> Can you please tell me how my if else statement would look like or any
> other way how to do that in R if my data frame name is "pt"
>
> Thanks
> Ana
> Ana
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Bert's answer is great, but since there is only two columns to be
used, why not simply
pt$pheno < pmax( p$phenoQ ,p$phenoH )
Cheers,
Lei
On Wed, 7 Aug 2019 at 21:23, Bert Gunter < [hidden email]> wrote:
>
> The ifelse() construction is fast, but after a couple of nested iterations,
> it gets messy and errorprone; so I believe to be avoided.
>
> In your case, there is a much better alternative, ?pmax . Ergo, something
> like:
>
> pt$pheno < do.call(pmax, pt[, 1])
>
> ?do.call is necessary to pass the list of columns pt[, 1] to pmax.
>
> Cheers,
> 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 Wed, Aug 7, 2019 at 11:55 AM Ana Marija < [hidden email]>
> wrote:
>
> > does this look ok:
> >
> > pt$pheno=ifelse(pt$phenoQ==9 & pt$phenoH==9,9,ifelse(pt$phenoH==2 
> > pt$phenoQ==2,2,1))
> >
> > On Wed, Aug 7, 2019 at 1:40 PM Ana Marija < [hidden email]>
> > wrote:
> > >
> > > Hello,
> > >
> > > I have a data frame which looks like this:
> > >
> > > > head(pt)
> > > eidQ phenoQ phenoH
> > > 1 1000017 9 9
> > > 2 1000025 9 9
> > > 3 1000038 9 1
> > > 4 1000042 9 9
> > > 5 1000056 9 9
> > > 6 1000074 9 9
> > > 7 1000038 9 1
> > > 8 1000127 2 1
> > > 9 1000690 2 9
> > > 10 1000711 2 9
> > > 11 1001431 2 1
> > > 12 1001710 9 1
> > >
> > > I would like to create the 3rd column called "pheno" which would have
> > > these values:
> > > 9,9,1,9,9,9,1,2,2,2,2,1
> > >
> > > so in other words:
> > > if 9 appears in both phenoQ and phenoH I will have 9 in pheno
> > > if 2 appears in any of phenoQ or phenoH I will have 2 in pheno
> > > if I have 9 and 1 or 1 and 9 in those columns I will have 1 in pheno
> > > if I have 9 and 2 or 2 and 9 in those columns I will have 2 in pheno
> > >
> > > Can you please tell me how my if else statement would look like or any
> > > other way how to do that in R if my data frame name is "pt"
> > >
> > > Thanks
> > > Ana
> > > Ana
> >
> > ______________________________________________
> > [hidden email] mailing list  To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/rhelp> > PLEASE do read the posting guide
> > http://www.Rproject.org/postingguide.html> > and provide commented, minimal, selfcontained, reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

