[R] how to add a row vector in a dataframe
arun
smartpink111 at yahoo.com
Sun Apr 14 00:00:07 CEST 2013
Hi,
Using S=1000
and
simdata <- replicate(S, generate(3000))
#If you want both "m1" and "m0" #here the missing values are 0
res1<-sapply(seq_len(ncol(simdata.psm1)),function(i) {x1<-merge(simdata.psm0[,i],simdata.psm1[,i],all=TRUE); x1[is.na(x1)]<-0; x1})
res1[,997:1000]
# [,1] [,2] [,3] [,4]
#x1 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x2 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x3 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x4 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x5 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x6 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x7 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x8 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x9 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x10 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#t Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#y Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#pshat Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#m0 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#m1 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#or if you wanted to need only one column "m". Here both columns "m0" and "m1" are merged. So no zeros.
res2<- sapply(seq_len(ncol(simdata.psm1)),function(i) {x1<-merge(simdata.psm0[,i],simdata.psm1[,i],all=TRUE); x1[is.na(x1)]<-0; x1$m0[x1$m0==0]<- x1$m1[x1$m1!=0];x2<-x1[,-15]; colnames(x2)[14]<-"m"; x2 })
res2[,997:1000]
# [,1] [,2] [,3] [,4]
#x1 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x2 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x3 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x4 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x5 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x6 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x7 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x8 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x9 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#x10 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#t Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#y Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#pshat Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
#m Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
A.K.
>hi. i am doing my assignment in school and you helped me a lot in
performing this to R and i am very thankful about that. however, I have
some >additional questions for this if you don't mind. i really need it.
this is the data:
>
>set.seed(12345)
>S=1000
>generate <- function(size) {
>x1 <- rnorm(size, mean=0, sd=1)
>x2 <- rnorm(size, mean=0, sd=1)
>x3 <- rnorm(size, mean=0, sd=1)
>x4 <- rnorm(size, mean=0, sd=1)
>x5 <- rnorm(size, mean=0, sd=1)
>x6 <- rnorm(size, mean=0, sd=1)
>x7 <- rnorm(size, mean=0, sd=1)
>x8 <- rnorm(size, mean=0, sd=1)
>x9 <- rnorm(size, mean=0, sd=1)
>x10 <- rnorm(size, mean=0, sd=1)
>e<-rnorm(size, mean=0, sd=1)
>t_trueps <- (1 + exp( -(b0 + b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7
>+ b2*x2*x2 + b4*x4*x4 + b7*x7*x7 + b1*0.5*x1*x3 + b2*0.7*x2*x4 +b3*0.5*x3*x5
>+ b4*0.7*x4*x6 + b5*0.5*x5*x7 + b1*0.5*x1*x6 + b2*0.7*x2*x3 + b3*0.5*x3*x4
>+ b4*0.5*x4*x5 + b5*0.5*x5*x6) ) )^-1
>prob.exposure <- runif(size)
>t <- ifelse(t_trueps > prob.exposure, 1, 0)
>y <- a0 + a1*x1 + a2*x2 + a3*x3 + a4*x4 +a5*x8 + a6*x9 + a7*x10 + g1*t + e
>sim <- as.data.frame(cbind(x1, x2, x3 ,x4, x5, x6, x7, x8, x9, x10, t, y))
>return(sim)
>}
>b0 <- 0.05
>b1 <- 0.95
>b2 <- -0.25
>b3 <- 0.6
>b4 <- -0.4
>b5 <- -0.8
>b6 <- -0.5
>b7 <- 0.7
>a0 <- -3.85
>a1 <- 0.3
>a2 <- -0.36
>a3 <- -0.73
>a4 <- -0.2
>a5 <- 0.71
>a6 <- -0.19
>a7 <- 0.26
>g1 <- -0.4
>simdata <- replicate(S, generate(3000))
>
>lstpshat<-lapply(seq_len(ncol(simdata)),function(i)
>{glm.t<-glm(t~x1+x2+x3+x4+x5+x6+x7+I(x2^2)+I(x4^2)+I(x7^2)+x1:x3+x2:x4+x3:x5+x4:x6+x5:x7+x1:x6+x2:x3+x3:x4+x4:x5+x5:x6,family=binomial,data=simdata[,i]);
>pshat<- predict(glm.t,type="response")})
>simdata1<-rbind(simdata,pshat=lstpshat)
>
>simdata.ps1<- simdata1
>simdata.ps1[]<-do.call(c,lapply(seq_len(ncol(simdata1)),function(i) lapply(simdata1[,i],function(x) x[simdata1[,i]$t==1])))
>lstm1<- lapply(seq_len(ncol(simdata.ps1)),function(i)
{dat<-do.call(data.frame,lapply(simdata.ps1[,i],function(x)
x));if(nrow(dat)!=0) >{glm.1<-glm(y~x1+x2+x3+x4+x8+x9+x10,data=dat)}
else NULL; glm.1; m1<- predict(glm.1)})
>
>simdata.ps0<- simdata1
>simdata.ps0[]<-do.call(c,lapply(seq_len(ncol(simdata1)),function(i) lapply(simdata1[,i],function(x) x[simdata1[,i]$t==0])))
>lstm0<-lapply(seq_len(ncol(simdata.ps0)),function(i)
{dat<-do.call(data.frame,lapply(simdata.ps0[,i],function(x)
x));if(nrow(dat)!=0) >{glm.0<-glm(y~x1+x2+x3+x4+x8+x9+x10,data=dat)}
else NULL; glm.0; m0<- predict(glm.0)})
>
>simdata.psm1<- rbind(simdata.ps1,m1=lstm1)
>simdata.psm0<- rbind(simdata.ps0,m0=lstm0)
>
>I want to combine or merge in a single dataset the simdata.psm1
and simdata.psm0, and also if value of m1 is not available, make it zero
and if >m0 is not available make it also zero. how will i perform those?
i made it a ahile ago but i get, for instance,
>
>Numeric,1429 Numeric,1467 Numeric,1517 Numeric,1434 Numeric,1519 Numeric,1418 Numeric,1423 Numeric,1491 Numeric,1472
> [,997] [,998] [,999] [,1000]
>x1 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x2 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x3 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x4 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x5 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x6 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x7 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x8 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x9 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x10 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>t Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>y Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>pshat Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>m1 Numeric,1549 Numeric,1533 Numeric,1484 Numeric,1548
>x1 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x2 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x3 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x4 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x5 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x6 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x7 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x8 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x9 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>x10 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>t Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>y Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>pshat Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>m0 Numeric,1451 Numeric,1467 Numeric,1516 Numeric,1452
>
>what i want is
>
> [,997] [,998] [,999] [,1000]
>x1 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x2 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x3 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x4 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x5 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x6 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x7 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x8 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x9 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>x10 Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>t Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>y Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>pshat Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>m Numeric,3000 Numeric,3000 Numeric,3000 Numeric,3000
>
>where m includes the m0 and m1 (but m0=0 if not available, that is m1 is available and similarly to m1)
>
>thanks!
More information about the R-help
mailing list