Skip to content

help with reshaping wide to long format

4 messages · usha2013, arun

#
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:

            
--
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.
2 days later
#
HI Usha,
I tried the codes on the full dataset.? This is what I get:
BP_2b<-read.csv("BP_2b.csv",sep="\t")
#head(BP_2b,2)
#? CODEA Sex MaternalAge Education Birthplace AggScore IntScore Obese14
#1???? 1? NA?????????? 3???????? 4????????? 1?????? NA?????? NA????? NA
#2???? 3?? 2?????????? 3???????? 3????????? 1??????? 0??????? 0?????? 0
?# Overweight14 Overweight21 Obese21 hibp14 hibp21
#1?????????? NA?????????? NA????? NA???? NA???? NA
#2??????????? 0??????????? 1?????? 0????? 0????? 0
BP.stack1<-
 reshape(BP_2b,sep="",timevar= 
"time",direction="long",varying=list(names(BP_2b[8:9]),names(BP_2b[10:11]),names(BP_2b[12:13])),v.names=c("Obese","Overweight","HiBP"),idvar="CODEA")

BP_2b<-BP_2b[,c(1:8,11,9:10,12:13)] #Obese21 was not adjacent to Obese14.? So, I rearranged the columns to make it adjacent.
str(BP_2b)
#'data.frame':??? 6898 obs. of? 13 variables:
# $ CODEA?????? : int? 1 3 4 7 8 9 10 11 12 13 ...
# $ Sex???????? : int? NA 2 2 2 2 1 NA 1 1 2 ...
# $ MaternalAge : int? 3 3 3 4 4 3 3 3 3 3 ...
# $ Education?? : int? 4 3 6 6 4 6 3 4 4 4 ...
# $ Birthplace? : int? 1 1 1 1 1 2 1 1 2 2 ...
# $ AggScore??? : int? NA 0 NA 0 0 0 NA 0 NA NA ...
# $ IntScore??? : int? NA 0 NA 0 0 0 NA 0 NA NA ...
# $ Obese14???? : int? NA 0 NA 0 0 0 NA NA NA NA ...
# $ Obese21???? : int? NA 0 NA 1 0 0 NA 0 NA NA ...
# $ Overweight14: int? NA 0 NA 0 0 0 NA NA NA NA ...
# $ Overweight21: int? NA 1 NA 1 0 0 NA 0 NA NA ...
# $ hibp14????? : int? NA 0 NA 0 0 0 NA NA NA NA ...
# $ hibp21????? : int? NA 0 NA 0 0 0 NA 0 NA NA ...

BP.stack2<-
 reshape(BP_2b,sep="",timevar= 
"time",direction="long",varying=list(names(BP_2b[8:9]),names(BP_2b[10:11]),names(BP_2b[12:13])),v.names=c("Obese","Overweight","HiBP"),idvar="CODEA")
?identical(BP.stack1,BP.stack2)
#[1] FALSE

library(car)
BP.stack2$time <- recode(BP.stack2$time,("1=14;2=21")) 
head(BP.stack2,2)
#??? CODEA Sex MaternalAge Education Birthplace AggScore IntScore time Obese
#1.1???? 1? NA?????????? 3???????? 4????????? 1?????? NA?????? NA?? 14??? NA
#3.1???? 3?? 2?????????? 3???????? 3????????? 1??????? 0??????? 0?? 14???? 0
#??? Overweight HiBP
#1.1???????? NA?? NA
#3.1????????? 0??? 0
?tail(BP.stack2,2)
#?????? CODEA Sex MaternalAge Education Birthplace AggScore IntScore time Obese
#8555.2? 8555?? 1?????????? 3???????? 6????????? 2??????? 0??????? 0?? 21??? NA
#8556.2? 8556?? 1?????????? 3???????? 4????????? 1??????? 0??????? 0?? 21???? 0
#?????? Overweight HiBP
#8555.2???????? NA?? NA
#8556.2????????? 0?? NA
nrow(BP.stack2)
#[1] 13796
?length(BP.stack2$time[BP.stack2$time==14])
#[1] 6898
?length(BP.stack2$time[BP.stack2$time==21])
#[1] 6898

#or
names(BP_2b)[grep("\\d+",names(BP_2b))]<-gsub("(\\D+)(\\d+)","\\1_\\2",names(BP_2b)[grep("\\d+",names(BP_2b))])
BP.stack3<-reshape(BP_2b,dir="long",varying=8:13,sep="_")
?head(BP.stack3,2)
#???? CODEA Sex MaternalAge Education Birthplace AggScore IntScore time Obese
#1.14???? 1? NA?????????? 3???????? 4????????? 1?????? NA?????? NA?? 14??? NA
#2.14???? 3?? 2?????????? 3???????? 3????????? 1??????? 0??????? 0?? 14???? 0
#???? Overweight hibp id
#1.14???????? NA?? NA? 1
#2.14????????? 0??? 0? 2
?tail(BP.stack3,2)
#??????? CODEA Sex MaternalAge Education Birthplace AggScore IntScore time Obese
#6897.21? 8555?? 1?????????? 3???????? 6????????? 2??????? 0??????? 0?? 21??? NA
#6898.21? 8556?? 1?????????? 3???????? 4????????? 1??????? 0??????? 0?? 21???? 0
#??????? Overweight hibp?? id
#6897.21???????? NA?? NA 6897
#6898.21????????? 0?? NA 6898
?nrow(BP.stack3)
#[1] 13796
?length(BP.stack3$time[BP.stack3$time==21])
#[1] 6898
?length(BP.stack3$time[BP.stack3$time==14])
#[1] 6898

#So, I guess this solves the problem.? Regarding your second question, 
"Do children who are obese or overweight at age 14 experience different 
change in blood pressure by 21 years, compared to those with normal 
weight at age 14?"

In
 the Obese or Overweight columns, I am not sure what "0" means.? For 
example, in the overweight column, does "0" means it is normal, or it 
has to be both "0" for overweight and obese for a particular ID (CODEA) 
in each of the age groups?? I guess it should be the latter.? If that is
 the case, subset data in to normal, Obese, Overweight and then do the 
comparisons.
For example:
?BP.stacknormal<-subset(BP.stack3,Obese==0 & Overweight==0)
?nrow(BP.stacknormal)
[1] 4371
?head(BP.stacknormal,3)
#???? CODEA Sex MaternalAge Education Birthplace AggScore IntScore time Obese
#2.14???? 3?? 2?????????? 3???????? 3????????? 1??????? 0??????? 0?? 14???? 0
#4.14???? 7?? 2?????????? 4???????? 6????????? 1??????? 0??????? 0?? 14???? 0
#5.14???? 8?? 2?????????? 4???????? 4????????? 1??????? 0??????? 0?? 14???? 0
? # ? Overweight hibp id
#2.14????????? 0??? 0? 2
#4.14????????? 0??? 0? 4
#5.14????????? 0??? 0? 5

Similarly, you need to subset for Obese==1 , Overweight==1
BP.stackObese <-subset(BP.stack3,Obese==1)
?nrow(BP.stackObese)
#[1] 530

Regarding the analysis, I think logistic regression should fit in this case because the response variable (hibp) is binary. 
#Just a simple logistic regression
BP.stacknormal$Categ<-"Normal"
?BP.stackObese$Categ<-"Obese"
?BP.newObeseNormal<-rbind(BP.stacknormal,BP.stackObese)
BP.newObeseNormal$time<-factor(BP.newObeseNormal$time)
BP.newObeseNormal$Categ<- factor(BP.newObeseNormal$Categ)


BPlogit<-glm(hibp~time+Categ,data=BP.newObeseNormal,family="binomial")
#or? BPlogit<-glm(hibp~time+Categ,data=BP.newObeseNormal,family=binomial(logit))

?summary(BPlogit)
#
#Call:
#glm(formula = hibp ~ time + Categ, family = "binomial", data = BP.newObeseNormal)
#
#Deviance Residuals: 
?# ? Min?????? 1Q?? Median?????? 3Q????? Max? 
#-0.8630? -0.5452? -0.5452? -0.4965?? 2.0759? 

#Coefficients:
?# ????????? Estimate Std. Error z value Pr(>|z|)??? 
#(Intercept) -1.83099??? 0.05768 -31.745?? <2e-16 ***
#time21????? -0.20038??? 0.09290? -2.157??? 0.031 *? 
#CategObese?? 1.03506??? 0.12046?? 8.593?? <2e-16 ***
#---
#Signif. codes:? 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 

For
 your question, I guess you need to add interaction term also in the 
model.? I am not sure whether you need random effects in the model.? If 
that is the case, you need ?lmer() from library(lme4). You could also 
post the question in R mixed models (r-sig-mixed-models at r-project.org) mailing list.
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:

            
--
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.