David wrote:
> Hello again allstat,
> Your help is needed again. Thanks for your help with the initial question. See below.
> # Dataset
> case <- 1:50
> x<-case*100
> y<-case/100
> d<-data.frame(case,x,y)
>
> sub<-d[x>=2500,]
> sub
> # sample 10 cases with replacement from sub
> s<-sample(sub$case,size=10,replace=TRUE)
>
> # why does d2 fail ?? Is d3 the only solution (which oddly needs to refer back to d) ??
> d2<-sub[s,]
> d2
> d3<-d[s,]
> d3
>
Hello,
d3 "works" for wrong reasons. When you use the [ extractor with numbers,
you are indicating the rows that you want to keep. This should work:
d4 <- subset( sub, case %in% s)
d4 <- sub[ sub$case %in% s, ]
Do you know that R has help list which are very efficient ? See
http://www.r-project.org/posting-guide.html
Cheers,
Romain
--
Mango Solutions
data analysis that delivers
Introduction to R training course :: London :: 06-07 March 2008
http://www.mango-solutions.com/services/rtraining/r_intro.html
>
>
>
> ========================================
> Message Received: Feb 14 2008, 04:33 PM
> From: "David"
> To: [log in to unmask]
> Cc:
> Subject: R question
>
> Hi allstat,
> I need help with the following R dataset manipulation problem. No doubt its easy!?? Thanks if you can help.
> # pretent dataset
> case <- 1:50
> x<-case*100
> y<-case/100
> d<-data.frame(case,x,y)
> # sample 10 cases with replacement from d
> s<-sample(case,size=10,replace=TRUE)
> # now want to create a dafaset of length 10 rows
> # of the same form "case,x,y" but corresponding to the sampled x's
> # Eg. for s=(12 12 14 12 11 37 47 35 11 22 14)
> # want ...
> # 12 1200 .12
> # 12 1200 .12
> # 14 1400 .14
> # 12 1200 .12
> # etc....
> thanks
> David
>
>
|