Hello, I have a data frame like this:> head(b)FID IID FLASER PLASER 1: fam1000 G1000 1 1 2: fam1001 G1001 1 1 3: fam1003 G1003 1 2 4: fam1005 G1005 1 1 5: fam1009 G1009 NA 2 6: fam1052 G1052 1 1 ...> unique(b$PLASER)[1] 1 2 NA> unique(b$FLASER)[1] 1 2 NA how can I do if else statement so that I am creating a PHENO =2 if b$FLASER=2 or b$PLASER=2 PHENO=1 if b$FLASER=1 and b$PLASER=1 otherwise PHENO=NA I tried this but I am not sure if this is correct: b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 | b$FLASER==2,2,NA)) Thanks Ana [[alternative HTML version deleted]]
"I tried this but I am not sure if this is correct:" Does it provide the expected result for all possible combinations of 1/2/NA for both variables? On Mon, May 4, 2020 at 1:16 PM Ana Marija <sokovic.anamarija at gmail.com> wrote:> Hello, > > I have a data frame like this: > > > head(b) > FID IID FLASER PLASER > 1: fam1000 G1000 1 1 > 2: fam1001 G1001 1 1 > 3: fam1003 G1003 1 2 > 4: fam1005 G1005 1 1 > 5: fam1009 G1009 NA 2 > 6: fam1052 G1052 1 1 > ... > > unique(b$PLASER) > [1] 1 2 NA > > unique(b$FLASER) > [1] 1 2 NA > > how can I do if else statement so that I am creating a > PHENO =2 if b$FLASER=2 or b$PLASER=2 > PHENO=1 if b$FLASER=1 and b$PLASER=1 > otherwise PHENO=NA > > I tried this but I am not sure if this is correct: > b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 | > b$FLASER==2,2,NA)) > > Thanks > Ana > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Patrick S. Malone, Ph.D., Malone Quantitative NEW Service Models: http://malonequantitative.com He/Him/His [[alternative HTML version deleted]]
To expand on Patrick's response... You can use the expand.grid function to generate a test table containing all combinations. However, we would not be in a position to verify that the results you get when you apply your logic to the test table are what you want... you know the requirements much better than we do. Nor is that kind of service what this mailing list is for, so please focus on showing what you cannot figure out how to accomplish rather than asking us to do or check your work for you. On May 4, 2020 10:33:12 AM PDT, "Patrick (Malone Quantitative)" <malone at malonequantitative.com> wrote:>"I tried this but I am not sure if this is correct:" > >Does it provide the expected result for all possible combinations of >1/2/NA >for both variables? > >On Mon, May 4, 2020 at 1:16 PM Ana Marija <sokovic.anamarija at gmail.com> >wrote: > >> Hello, >> >> I have a data frame like this: >> >> > head(b) >> FID IID FLASER PLASER >> 1: fam1000 G1000 1 1 >> 2: fam1001 G1001 1 1 >> 3: fam1003 G1003 1 2 >> 4: fam1005 G1005 1 1 >> 5: fam1009 G1009 NA 2 >> 6: fam1052 G1052 1 1 >> ... >> > unique(b$PLASER) >> [1] 1 2 NA >> > unique(b$FLASER) >> [1] 1 2 NA >> >> how can I do if else statement so that I am creating a >> PHENO =2 if b$FLASER=2 or b$PLASER=2 >> PHENO=1 if b$FLASER=1 and b$PLASER=1 >> otherwise PHENO=NA >> >> I tried this but I am not sure if this is correct: >> b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 | >> b$FLASER==2,2,NA)) >> >> Thanks >> Ana >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >>-- Sent from my phone. Please excuse my brevity.
Hello, Here is a way, using logical indices. b$pheno <- NA b$pheno[b$FLASER == 1 & b$PLASER == 1] <- 1 b$pheno[b$FLASER == 2 | b$PLASER == 2] <- 2 Hope this helps, Rui Barradas ?s 18:15 de 04/05/20, Ana Marija escreveu:> Hello, > > I have a data frame like this: > >> head(b) > FID IID FLASER PLASER > 1: fam1000 G1000 1 1 > 2: fam1001 G1001 1 1 > 3: fam1003 G1003 1 2 > 4: fam1005 G1005 1 1 > 5: fam1009 G1009 NA 2 > 6: fam1052 G1052 1 1 > ... >> unique(b$PLASER) > [1] 1 2 NA >> unique(b$FLASER) > [1] 1 2 NA > > how can I do if else statement so that I am creating a > PHENO =2 if b$FLASER=2 or b$PLASER=2 > PHENO=1 if b$FLASER=1 and b$PLASER=1 > otherwise PHENO=NA > > I tried this but I am not sure if this is correct: > b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 | > b$FLASER==2,2,NA)) > > Thanks > Ana > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Your ifelse expression looks fine. What goes wrong with it? On Tue, 5 May 2020 at 05:16, Ana Marija <sokovic.anamarija at gmail.com> wrote:> > Hello, > > I have a data frame like this: > > > head(b) > FID IID FLASER PLASER > 1: fam1000 G1000 1 1 > 2: fam1001 G1001 1 1 > 3: fam1003 G1003 1 2 > 4: fam1005 G1005 1 1 > 5: fam1009 G1009 NA 2 > 6: fam1052 G1052 1 1 > ... > > unique(b$PLASER) > [1] 1 2 NA > > unique(b$FLASER) > [1] 1 2 NA > > how can I do if else statement so that I am creating a > PHENO =2 if b$FLASER=2 or b$PLASER=2 > PHENO=1 if b$FLASER=1 and b$PLASER=1 > otherwise PHENO=NA > > I tried this but I am not sure if this is correct: > b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 | > b$FLASER==2,2,NA)) > > Thanks > Ana > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hi another possible version b$pheno <- ((b$FLASER==2) | (b$PLASER==2))+1 Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Rui Barradas > Sent: Monday, May 4, 2020 8:32 PM > To: sokovic.anamarija at gmail.com; r-help <r-help at r-project.org> > Subject: Re: [R] if else statement > > Hello, > > Here is a way, using logical indices. > > b$pheno <- NA > b$pheno[b$FLASER == 1 & b$PLASER == 1] <- 1 b$pheno[b$FLASER == 2 | > b$PLASER == 2] <- 2 > > > Hope this helps, > > Rui Barradas > > ?s 18:15 de 04/05/20, Ana Marija escreveu: > > Hello, > > > > I have a data frame like this: > > > >> head(b) > > FID IID FLASER PLASER > > 1: fam1000 G1000 1 1 > > 2: fam1001 G1001 1 1 > > 3: fam1003 G1003 1 2 > > 4: fam1005 G1005 1 1 > > 5: fam1009 G1009 NA 2 > > 6: fam1052 G1052 1 1 > > ... > >> unique(b$PLASER) > > [1] 1 2 NA > >> unique(b$FLASER) > > [1] 1 2 NA > > > > how can I do if else statement so that I am creating a PHENO =2 if > > b$FLASER=2 or b$PLASER=2 > > PHENO=1 if b$FLASER=1 and b$PLASER=1 > > otherwise PHENO=NA > > > > I tried this but I am not sure if this is correct: > > b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 | > > b$FLASER==2,2,NA)) > > > > Thanks > > Ana > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > 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. > > > > ______________________________________________ > 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.