[R] help with reshaping wide to long format
arun
smartpink111 at yahoo.com
Fri Dec 28 14:23:56 CET 2012
Hi,
DId you tried "copy and paste" the code in my reply? If not, please do.
In your previous replies, you forgot to close the bracket(")") or adding "#" in between the codes. Please check your codes.
It would be also helpful to read the posting guide (http://www.r-project.org/posting-guide.html) and "An introduction to R" (the link will be in the posting guide).
A.K.
----- Original Message -----
From: usha2013 <usha.nathan at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, December 28, 2012 2:02 AM
Subject: Re: [R] help with reshaping wide to long format
Hi, Sorry, but how did you bring it out?
Thanks
On Fri, Dec 28, 2012 at 8:48 AM, arun kirshna [via R] <
ml-node+s789695n4654093h10 at n4.nabble.com> wrote:
> Hi,
> bp.sub<- structure(list(CODEA = c(1L, 3L, 4L, 7L, 8L, 9L, 10L, 11L, 12L,
> 13L, 14L, 16L, 17L), C45 = c(NA, 2L, 2L, 2L, 2L, 1L, NA, 1L,
> 1L, 2L, 1L, 2L, 1L), ragek = c(3L, 3L, 3L, 4L, 4L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L), ra80 = c(4L, 3L, 6L, 6L, 4L, 6L, 3L, 4L,
> 4L, 4L, 4L, 4L, 4L), ra98 = c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
> 2L, 2L, 2L, 3L, 1L), CBCLAggressionAt1410 = c(NA, 0L, NA, 0L,
> 0L, 0L, NA, 0L, NA, NA, 0L, 0L, NA), CBCLInternalisingAt1410 = c(NA,
> 0L, NA, 0L, 0L, 0L, NA, 0L, NA, NA, 0L, 0L, NA), Obese14 = c(NA,
> 0L, NA, 0L, 0L, 0L, NA, NA, NA, NA, 0L, 0L, NA), Obese21 = c(NA,
> 0L, NA, 1L, 0L, 0L, NA, 0L, NA, NA, 0L, 0L, NA), Overweight14 = c(NA,
> 0L, NA, 0L, 0L, 0L, NA, NA, NA, NA, 0L, 0L, NA), Overweight21 = c(NA,
> 1L, NA, 1L, 0L, 0L, NA, 0L, NA, NA, 1L, 0L, NA), hibp14 = c(NA,
> 0L, NA, 0L, 0L, 0L, NA, NA, NA, NA, 1L, 1L, NA), hibp21 = c(NA,
> 0L, NA, 0L, 0L, 0L, NA, 0L, NA, NA, 1L, NA, NA)), .Names = c("CODEA",
> "C45", "ragek", "ra80", "ra98", "CBCLAggressionAt1410",
> "CBCLInternalisingAt1410",
> "Obese14", "Obese21", "Overweight14", "Overweight21", "hibp14",
> "hibp21"), row.names = c(NA, 13L), class = "data.frame")
>
> BP.stack1<- reshape(bp.sub,sep="",timevar=
> "time",direction="long",varying=list(names(bp.sub[8:9]),names(bp.sub[10:11]),names(bp.sub[12:13])),v.names=c("Obese","Overweight","HiBP"),idvar="CODEA")
>
> BP.stack1$time <- recode(BP.stack1$time,("1=14;2=21"))
> BP.stack1
> # CODEA C45 ragek ra80 ra98 CBCLAggressionAt1410
> CBCLInternalisingAt1410
> #1.1 1 NA 3 4 1 NA
> NA
> #3.1 3 2 3 3 1 0
> 0
> #4.1 4 2 3 6 1 NA
> NA
> #7.1 7 2 4 6 1 0
> 0
> #8.1 8 2 4 4 1 0
> 0
> #9.1 9 1 3 6 2 0
> 0
> #10.1 10 NA 3 3 1 NA
> NA
> #11.1 11 1 3 4 1 0
> 0
> #12.1 12 1 3 4 2 NA
> NA
> #13.1 13 2 3 4 2 NA
> NA
> #14.1 14 1 3 4 2 0
> 0
> #16.1 16 2 3 4 3 0
> 0
> #17.1 17 1 3 4 1 NA
> NA
> #1.2 1 NA 3 4 1 NA
> NA
> #3.2 3 2 3 3 1 0
> 0
> #4.2 4 2 3 6 1 NA
> NA
> #7.2 7 2 4 6 1 0
> 0
> #8.2 8 2 4 4 1 0
> 0
> #9.2 9 1 3 6 2 0
> 0
> #10.2 10 NA 3 3 1 NA
> NA
> #11.2 11 1 3 4 1 0
> 0
> #12.2 12 1 3 4 2 NA
> NA
> #13.2 13 2 3 4 2 NA
> NA
> #14.2 14 1 3 4 2 0
> 0
> #16.2 16 2 3 4 3 0
> 0
> #17.2 17 1 3 4 1 NA
> NA
> # time Obese Overweight HiBP
> #1.1 14 NA NA NA
> #3.1 14 0 0 0
> #4.1 14 NA NA NA
> #7.1 14 0 0 0
> #8.1 14 0 0 0
> #9.1 14 0 0 0
> #10.1 14 NA NA NA
> #11.1 14 NA NA NA
> #2.1 14 NA NA NA
> #13.1 14 NA NA NA
> #14.1 14 0 0 1
> #16.1 14 0 0 1
> #17.1 14 NA NA NA
> #1.2 21 NA NA NA
> #3.2 21 0 1 0
> #4.2 21 NA NA NA
> #7.2 21 1 1 0
> #8.2 21 0 0 0
> #9.2 21 0 0 0
> #10.2 21 NA NA NA
> #11.2 21 0 0 0
> #12.2 21 NA NA NA
> #13.2 21 NA NA NA
> #14.2 21 0 1 1
> #16.2 21 0 0 NA
> #17.2 21 NA NA NA
> names(bp.sub)[grep("\\d+",names(bp.sub))]<-gsub("(\\D+)(\\d+)","\\1_\\2",names(bp.sub)[grep("\\d+",names(bp.sub))])
>
> BP.stack2<-reshape(bp.sub,dir="long",varying=8:13,sep="_")
> Bp.stack2
> CODEA C_45 ragek ra_80 ra_98 CBCLAggressionAt_1410
> 1.14 1 NA 3 4 1 NA
> 2.14 3 2 3 3 1 0
> 3.14 4 2 3 6 1 NA
> 4.14 7 2 4 6 1 0
> 5.14 8 2 4 4 1 0
> 6.14 9 1 3 6 2 0
> 7.14 10 NA 3 3 1 NA
> 8.14 11 1 3 4 1 0
> 9.14 12 1 3 4 2 NA
> 10.14 13 2 3 4 2 NA
> 11.14 14 1 3 4 2 0
> 12.14 16 2 3 4 3 0
> 13.14 17 1 3 4 1 NA
> 1.21 1 NA 3 4 1 NA
> 2.21 3 2 3 3 1 0
> 3.21 4 2 3 6 1 NA
> 4.21 7 2 4 6 1 0
> 5.21 8 2 4 4 1 0
> 6.21 9 1 3 6 2 0
> 7.21 10 NA 3 3 1 NA
> 8.21 11 1 3 4 1 0
> 9.21 12 1 3 4 2 NA
> 10.21 13 2 3 4 2 NA
> 11.21 14 1 3 4 2 0
> 12.21 16 2 3 4 3 0
> 13.21 17 1 3 4 1 NA
> CBCLInternalisingAt_1410 time Obese Overweight hibp id
> 1.14 NA 14 NA NA NA 1
> 2.14 0 14 0 0 0 2
> 3.14 NA 14 NA NA NA 3
> 4.14 0 14 0 0 0 4
> 5.14 0 14 0 0 0 5
> 6.14 0 14 0 0 0 6
> 7.14 NA 14 NA NA NA 7
> 8.14 0 14 NA NA NA 8
> 9.14 NA 14 NA NA NA 9
> 10.14 NA 14 NA NA NA 10
> 11.14 0 14 0 0 1 11
> 12.14 0 14 0 0 1 12
> 13.14 NA 14 NA NA NA 13
> 1.21 NA 21 NA NA NA 1
> 2.21 0 21 0 1 0 2
> 3.21 NA 21 NA NA NA 3
> 4.21 0 21 1 1 0 4
> 5.21 0 21 0 0 0 5
> 6.21 0 21 0 0 0 6
> 7.21 NA 21 NA NA NA 7
> 8.21 0 21 0 0 0 8
> 9.21 NA 21 NA NA NA 9
> 10.21 NA 21 NA NA NA 10
> 11.21 0 21 0 1 1 11
> 12.21 0 21 0 0 NA 12
> 13.21 NA 21 NA NA NA 13
> A.K.
>
>
>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://r.789695.n4.nabble.com/help-with-reshaping-wide-to-long-format-tp4653922p4654093.html
> To unsubscribe from help with reshaping wide to long format, click here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4653922&code=dXNoYS5uYXRoYW5AZ21haWwuY29tfDQ2NTM5MjJ8MjAyMjE1NDI0>
> .
> NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--
View this message in context: http://r.789695.n4.nabble.com/help-with-reshaping-wide-to-long-format-tp4653922p4654122.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
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.
More information about the R-help
mailing list