-1

I'am new using R. I have a data frame (nrow=10200, ncol=16) with 20 replications. I want to take random sample (e.g. 1000 rows) from each replication and make new data frame (20000x16). Those randomly sampled rows need to be deleted from the original data frames. I know how to random sample, but failed to delete the sampled rows from the original file while sampling. Any hint on how to cut out a random sample from a data frame?

T.Belay
  • 1
  • 2
  • Possible duplicate of [How do I delete rows in a data frame?](http://stackoverflow.com/questions/12328056/how-do-i-delete-rows-in-a-data-frame) – slamballais Feb 02 '16 at 20:26
  • Possible duplicate of [Sampling on factor in R](http://stackoverflow.com/questions/30097382/sampling-on-factor-in-r) – scoa Feb 03 '16 at 10:10

2 Answers2

0

What about

while(nrow(df)>=1000){
        smp <- sample(1:nrow(df),1000)
        df1[[length(df1)+1]] <- df[smp]
        df <- df[-smp]
}
HubertL
  • 19,246
  • 3
  • 32
  • 51
0

Thank you all for your response! I solved my problem using the following script. I also thank my friend B.Dagachew for writing me the script.

col<-matrix(0,nrow=20000,ncol=16)
se<-seq(1,20000,1000)

for(i in 1:20){
    df=read.table(paste("mspop.phen",i,sep=""), header=F, skip=1)
    sam=sample(nrow(df),1000)
    df_less<-df[-sam,]
    col[se[i]:(se[i]+999),]<-as.matrix(df[sam,])
    write.table(df_less,file=paste("pheno",i,sep=""), col.names=FALSE, row.names=FALSE, sep="\t")
    }

    write.table(col,file="sampled.d", col.names=FALSE, row.names=FALSE, sep="\t")
T.Belay
  • 1
  • 2