[R] FW: Select a random subset of rows out of matrix

Rui Barradas ruipbarradas at sapo.pt
Wed Nov 21 15:18:28 CET 2012


Hello,

It's not, in general, possible to fullfill the two constraints, to both 
select 80% of the rows and to select all x %in% sampled x. Maybe the 
following is a way close enough.

set.seed(244)
s <- sample(unique(dat$x), length(unique(dat$x))*0.8, prob = table(dat$x))
dat[dat$x %in% s,]


Hope this helps,

Rui Barradas
Em 21-11-2012 13:25, arun escreveu:
> HI,
> Your question is not very clear to me.
>
> dat1<-data.frame(x=c(1,1,2,2,2,3,4,4),y=c(23,45,87,46,78,12,87,79))
> If I select randomly 80% of data:
>   dat1[sample(nrow(dat1),0.8*nrow(dat1)),]
> #  x  y
> #4 2 46
> #6 3 12
> #7 4 87
> #3 2 87
> #2 1 45
> #8 4 79
>
> In this case, it is a mix of all the records that was in the original data.  So, if I understand correctly, then you want to get the original data, instead of the 80% data (as you want all the records).  But, suppose if the sample is:
>   set.seed(244)
>   dat2<-dat1[sample(nrow(dat1),0.8*nrow(dat1)),]
>   dat2
> #  x  y
> #2 1 45
> #7 4 87
> #8 4 79
> #1 1 23
> #5 2 78
> #4 2 46
>
> You wanted to eliminate the records 3 from the original dataset.  Is that what you meant?
> If that is the case:
> dat1[dat1$x%in%dat2$x,]
> #  x  y
> #1 1 23
> #2 1 45
> #3 2 87
> #4 2 46
> #5 2 78
> #7 4 87
> #8 4 79
>
> A.K.
>
>
>
>
>
> ----- Original Message -----
> From: Madhu Ganganapalli <mganganapalli at upstreamsoftware.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Wednesday, November 21, 2012 4:10 AM
> Subject: Re: [R] FW: Select a random subset of rows out of matrix
>
> Hi,
>    This is Madhu and I have a following doubt please give a solution...
>
> **>i have the following data frame
> from this i want to select a 80% of data randomly in such a way that
> if the selected records are 1 and then we have to get the all records corresponding to 1
> similarly for 2 also and soon......... help me
>
>      data<-data.frame(x=c(1,1,2,2,2,3,4,4,4),y=c(23,45,87,46,78,12,87,79));**
>
>
> Regards,
> Madhu.
>
>      [[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.
>
>
> ______________________________________________
> 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