Good day, #I?ve created hypothetical data for drug X and drug Y whereby both drug have the ability to have HbA1c reduction. set.seed(10) drugx= rnorm(50, mean = 0.1, sd=0.02) set.seed(11) drugy= rnorm(50, mean=0.15, sd=0.03) #And created a data frame, compare drugs (comdrug) of 50 patients for each drug. comdrug= data.frame("ID"=c(1:50), "DrugX"=drugx, "DrugY"=drugy) # whereby the data would look like this head(comdrug)> head(comdrug)ID DrugX DrugY 1 1 0.10037492 0.1322691 2 2 0.09631495 0.1507978 3 3 0.07257339 0.1045034 4 4 0.08801665 0.1091204 5 5 0.10589090 0.1853547 6 6 0.10779589 0.1219755 Is there anyway of coding if I could arrange like this?
I'm sorry, I don't understand what you're trying to do with your hypothetical data. Can you expand on what your question is? Sarah On Thu, Mar 21, 2019 at 9:54 AM Anaanthan Pillai <anaanthanpillai at gmail.com> wrote:> > Good day, > > #I?ve created hypothetical data for drug X and drug Y whereby both drug have the ability to have HbA1c reduction. > > set.seed(10) > drugx= rnorm(50, mean = 0.1, sd=0.02) > > set.seed(11) > drugy= rnorm(50, mean=0.15, sd=0.03) > > #And created a data frame, compare drugs (comdrug) of 50 patients for each drug. > > comdrug= data.frame("ID"=c(1:50), "DrugX"=drugx, "DrugY"=drugy) > > # whereby the data would look like this > > head(comdrug) > > > head(comdrug) > ID DrugX DrugY > 1 1 0.10037492 0.1322691 > 2 2 0.09631495 0.1507978 > 3 3 0.07257339 0.1045034 > 4 4 0.08801665 0.1091204 > 5 5 0.10589090 0.1853547 > 6 6 0.10779589 0.1219755 > > Is there anyway of coding if I could arrange like this? > > > > ______________________________________________ > 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.-- Sarah Goslee (she/her) http://www.numberwright.com
Please also copy the R-help email list when you reply. On Thu, Mar 21, 2019 at 10:05 AM Anaanthan Pillai <anaanthanpillai at gmail.com> wrote:> > The hypothetical data is about drug x and drug y both we assume as diabetic drug which can reduce hbaic level (sugar level) > > Currently I have a data frame of 3X50, whereby the columns is (ID, drug x?s hba1c reduction, drug y?s hba1c reduction) > > I want to reaarrange the data into ID, type of drugs (x or y) and their corresponding hba1c level.Probably you need the reshape2 package. Can you provide an example of what you expect the resulting data frame to look like? I'm assuming that your sample data is the starting point. Or do you simply need to rbind the two columns together? Sarah> Anand > > Begin forwarded message: > > From: Sarah Goslee <sarah.goslee at gmail.com> > Subject: Re: [R] Coding help for data frame. > Date: 21 March 2019 at 9:56:43 PM MYT > To: Anaanthan Pillai <anaanthanpillai at gmail.com> > Cc: r-help <r-help at r-project.org> > > I'm sorry, I don't understand what you're trying to do with your > hypothetical data. > > Can you expand on what your question is? > > Sarah > > On Thu, Mar 21, 2019 at 9:54 AM Anaanthan Pillai > <anaanthanpillai at gmail.com> wrote: > > > Good day, > > #I?ve created hypothetical data for drug X and drug Y whereby both drug have the ability to have HbA1c reduction. > > set.seed(10) > drugx= rnorm(50, mean = 0.1, sd=0.02) > > set.seed(11) > drugy= rnorm(50, mean=0.15, sd=0.03) > > #And created a data frame, compare drugs (comdrug) of 50 patients for each drug. > > comdrug= data.frame("ID"=c(1:50), "DrugX"=drugx, "DrugY"=drugy) > > # whereby the data would look like this > > head(comdrug) > > head(comdrug) > > ID DrugX DrugY > 1 1 0.10037492 0.1322691 > 2 2 0.09631495 0.1507978 > 3 3 0.07257339 0.1045034 > 4 4 0.08801665 0.1091204 > 5 5 0.10589090 0.1853547 > 6 6 0.10779589 0.1219755 > > Is there anyway of coding if I could arrange like this? > >-- Sarah Goslee (she/her) http://www.numberwright.com
> Hi, > > I?ve managed to sort out the problem. Yes you are correct, one of the coding method is using reshape2 > > The coding is as below: > >> library(tidyr) >> comdrug2 <- gather(comdrug, key = DrugType, value = Reduction, DrugX, DrugY) >> head(comdrug2) > ID DrugType Reduction > 1 1 DrugX 0.10037492 > 2 2 DrugX 0.09631495 > 3 3 DrugX 0.07257339 > 4 4 DrugX 0.08801665 > 5 5 DrugX 0.10589090 > 6 6 DrugX 0.10779589 >> tail(comdrug2) > ID DrugType Reduction > 95 45 DrugY 0.17670152 > 96 46 DrugY 0.13968377 > 97 47 DrugY 0.08439656 > 98 48 DrugY 0.17640175 > 99 49 DrugY 0.17171570 > 100 50 DrugY 0.15659558 > > > Alternatively could be done as: > > ## option 2 to combine drug effects into single columnn (Drugtype) > ## (will provide same result) > > comdrug_long <- reshape2::melt(comdrug, id.vars = "ID", > value.name = "HbA1c Reduction", > variable.name = "Types of Drug?) > > > Regards, > > Anaanthan > Begin forwarded message: > > From: Sarah Goslee <sarah.goslee at gmail.com> > Subject: Re: [R] Coding help for data frame. > Date: 21 March 2019 at 11:20:59 PM MYT > To: Anaanthan Pillai <anaanthanpillai at gmail.com>, r-help <r-help at r-project.org> > > Please also copy the R-help email list when you reply. > > On Thu, Mar 21, 2019 at 10:05 AM Anaanthan Pillai > <anaanthanpillai at gmail.com> wrote: >> >> The hypothetical data is about drug x and drug y both we assume as diabetic drug which can reduce hbaic level (sugar level) >> >> Currently I have a data frame of 3X50, whereby the columns is (ID, drug x?s hba1c reduction, drug y?s hba1c reduction) >> >> I want to reaarrange the data into ID, type of drugs (x or y) and their corresponding hba1c level. > > Probably you need the reshape2 package. Can you provide an example of > what you expect the resulting data frame to look like? I'm assuming > that your sample data is the starting point. > > Or do you simply need to rbind the two columns together? > > Sarah > > >> Anand >> >> Begin forwarded message: >> >> From: Sarah Goslee <sarah.goslee at gmail.com> >> Subject: Re: [R] Coding help for data frame. >> Date: 21 March 2019 at 9:56:43 PM MYT >> To: Anaanthan Pillai <anaanthanpillai at gmail.com> >> Cc: r-help <r-help at r-project.org> >> >> I'm sorry, I don't understand what you're trying to do with your >> hypothetical data. >> >> Can you expand on what your question is? >> >> Sarah >> >> On Thu, Mar 21, 2019 at 9:54 AM Anaanthan Pillai >> <anaanthanpillai at gmail.com> wrote: >> >> >> Good day, >> >> #I?ve created hypothetical data for drug X and drug Y whereby both drug have the ability to have HbA1c reduction. >> >> set.seed(10) >> drugx= rnorm(50, mean = 0.1, sd=0.02) >> >> set.seed(11) >> drugy= rnorm(50, mean=0.15, sd=0.03) >> >> #And created a data frame, compare drugs (comdrug) of 50 patients for each drug. >> >> comdrug= data.frame("ID"=c(1:50), "DrugX"=drugx, "DrugY"=drugy) >> >> # whereby the data would look like this >> >> head(comdrug) >> >> head(comdrug) >> >> ID DrugX DrugY >> 1 1 0.10037492 0.1322691 >> 2 2 0.09631495 0.1507978 >> 3 3 0.07257339 0.1045034 >> 4 4 0.08801665 0.1091204 >> 5 5 0.10589090 0.1853547 >> 6 6 0.10779589 0.1219755 >> >> Is there anyway of coding if I could arrange like this? >> >> > > -- > Sarah Goslee (she/her) > http://www.numberwright.com[[alternative HTML version deleted]]