I have a data set called yall with 5000 rows, I want to randomly sub sample 100 rows 100 times. This is what I have so far: yall<-read.csv("Z:\\SOFTEL\\North Key Largo project\\Canopy_Height\\random_age_strat\\Young\\Abv2ft_young.csv") *yall1 <- yall[sample(1:nrow(yall), 100, replace=FALSE),] write.csv(yall1, file = "yall1.csv")* I want to run a loop of the bold script above 100 times, but I want to be able to change the name of the sub sample and name of the csv file each time. They should be named yall1, yall2,yall3...etc until 100. -- View this message in context: http://r.789695.n4.nabble.com/Running-Loops-tp4675886.html Sent from the R help mailing list archive at Nabble.com.
Hi, Try: set.seed(24) yall<- as.data.frame(matrix(sample(1:1e5,5000*10,replace=FALSE),ncol=10)) ?set.seed(49) ? lst1<-replicate(100,yall[sample(1:nrow(yall),100,replace=FALSE),],simplify=FALSE) ?names(lst1)<- paste0("yall",1:100) lapply(seq_along(lst1),function(i) write.csv(lst1[[i]],file=paste0("yall",i,".csv"),row.names=FALSE)) A.K. I have a data set called yall with 5000 rows, I want to randomly sub sample 100 rows 100 times. This is what I have so far: yall<-read.csv("Z:\\SOFTEL\\North Key Largo project\\Canopy_Height\\random_age_strat\\Young\\Abv2ft_young.csv") yall1 <- yall[sample(1:nrow(yall), 100, replace=FALSE),] write.csv(yall1, file = "yall1.csv") I want to run a loop of the bold script above 100 times, but I want to be able to change the name of the sub sample and name of the csv file each time. They should be named yall1, yall2,yall3...etc until 100.
Hi Why not use loops something like lll<-vector("list", 100) for (i in 1:100) lll[[i]] <- yall[sample(1:nrow(yall), 100, replace=FALSE),] for(i in 1:100) write.csv(lll[[i]], file = paste("yall",i, sep="")) Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of jfrei006 > Sent: Wednesday, September 11, 2013 4:49 PM > To: r-help at r-project.org > Subject: [R] Running Loops > > I have a data set called yall with 5000 rows, I want to randomly sub > sample 100 rows 100 times. > This is what I have so far: > > yall<-read.csv("Z:\\SOFTEL\\North Key Largo > project\\Canopy_Height\\random_age_strat\\Young\\Abv2ft_young.csv") > > *yall1 <- yall[sample(1:nrow(yall), 100, replace=FALSE),] > > write.csv(yall1, file = "yall1.csv")* > > I want to run a loop of the bold script above 100 times, but I want to > be able to change the name of the sub sample and name of the csv file > each time. They should be named yall1, yall2,yall3...etc until 100. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Running- > Loops-tp4675886.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.