[R] Coding help for data frame.
Sarah Goslee
@@r@h@go@|ee @end|ng |rom gm@||@com
Thu Mar 21 16:48:20 CET 2019
I'm glad you figured it out.
You do still need to reply to the R-help list, not me, but because you
did reply just to me I discovered that your orignal question had an
image in it of your desired result, which was stripped by the
pliant-text R-help server.
So your original email ended with:
"Is there anyway of coding if I could arrange like this?"
with absolutely no way to know what that was referring to.
Please take a look at the posting guide for this list to better
understand how to frame good questions, so people can help you.
Sarah
On Thu, Mar 21, 2019 at 11:42 AM Anaanthan Pillai
<anaanthanpillai using gmail.com> wrote:
>
>
> 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: Anaanthan Pillai <anaanthanpillai using gmail.com>
> Subject: Coding help for data frame.
> Date: 21 March 2019 at 8:01:27 PM MYT
> To: r-help using r-project.org
>
> 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
More information about the R-help
mailing list