erinu
2013-Oct-23 18:13 UTC
[R] How to view un-sampled data from a randomly sampled dataset
Hi there- I have a 150 row dataset (data). I create "y" a randomly sampled (without replacement) set number of observations (40): y<-data[sample(1:nrow(data),40,replace=FALSE),] I would like to make a new variable "x" that contains the leftover non-sampled 110 observations. I am sure there is a fairly easy way to do this. Any help would be greatly appreciated. THANKS! -- View this message in context: http://r.789695.n4.nabble.com/How-to-view-un-sampled-data-from-a-randomly-sampled-dataset-tp4678887.html Sent from the R help mailing list archive at Nabble.com.
arun
2013-Oct-23 19:17 UTC
[R] How to view un-sampled data from a randomly sampled dataset
HI, You may try: set.seed(432) ?dat1 <- data.frame(Col1=1:150,Col2=rnorm(150)) ?y <- dat1[sample(1:nrow(dat1),40,replace=FALSE),] x <- dat1[-as.numeric(row.names(y)),] ?dim(x) #[1] 110?? 2 intersect(row.names(x),row.names(y)) #character(0) #or ?x1 <- dat1[!row.names(dat1) %in% row.names(y),] ?identical(x,x1) #[1] TRUE A.K. Hi there- I have a 150 row dataset (data). I create "y" a randomly sampled (without replacement) set number of observations (40): y<-data[sample(1:nrow(data),40,replace=FALSE),] I would like to make a new variable "x" that contains the leftover non-sampled 110 observations. ?I am sure there is a fairly easy way to do this. Any help would be greatly appreciated. THANKS!
peter dalgaard
2013-Oct-23 19:36 UTC
[R] How to view un-sampled data from a randomly sampled dataset
On Oct 23, 2013, at 20:13 , erinu wrote:> Hi there- > > I have a 150 row dataset (data). I create "y" a randomly sampled (without > replacement) set number of observations (40): > > y<-data[sample(1:nrow(data),40,replace=FALSE),] > > I would like to make a new variable "x" that contains the leftover > non-sampled 110 observations. I am sure there is a fairly easy way to do > this. > > Any help would be greatly appreciated. > > THANKS! >Just hold on to the indices: s <- sample(1:nrow(data), 40, replace=FALSE) y <- data[s,] x <- data[-s,] -pd> > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-view-un-sampled-data-from-a-randomly-sampled-dataset-tp4678887.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Nordlund, Dan (DSHS/RDA)
2013-Oct-23 19:39 UTC
[R] How to view un-sampled data from a randomly sampled dataset
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of erinu > Sent: Wednesday, October 23, 2013 11:14 AM > To: r-help at r-project.org > Subject: [R] How to view un-sampled data from a randomly sampled > dataset > > Hi there- > > I have a 150 row dataset (data). I create "y" a randomly sampled > (without > replacement) set number of observations (40): > > y<-data[sample(1:nrow(data),40,replace=FALSE),] > > I would like to make a new variable "x" that contains the leftover > non-sampled 110 observations. I am sure there is a fairly easy way to > do > this. > > Any help would be greatly appreciated. > > THANKS! > >Perhaps something like ndx <- sample(1:nrow(data),40,replace=FALSE) y <- data[ndx,] x <- data[-ndx,] hope this is helpful, Dan Daniel J. Nordlund, PhD Research and Data Analysis Division Services & Enterprise Support Administration Washington State Department of Social and Health Services