alice.0309
2005-Nov-10 21:38 UTC
[R] How to export multiple files using write.table in the loop?
Hi, I tried to split a big file into some small files seperately by R. I can only do that writing duplicated codes. When I tried to write a loop, I only got one appned or destroyed exported file. For example: data1<- read.table(file = "C:\\Alice\\MBEI.txt", sep="\t", check.names=FALSE) a<-subset(data1,select=c(V1,V2,V3)) write.table<-(a,file="C:\\Alice\\aa1.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") a<-subset(data1,select=c(V1,V4,V5)) write.table(a,file="C:\\Alice\\aa2.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") or data1<- read.table(file = "C:\\Alice\\MBEI.txt", sep="\t", check.names=FALSE) a<-data.frame(data1[,1],data1[,2],data1[,3] write.table<-(a,file="C:\\Alice\\aa1.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") a<-data.frame(data1[,1],data1[,4],data1[,5] write.table(a,file="C:\\Alice\\aa2.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") I tried to write a loop for it like : i <- 1 while (i <3) { qq<-data.frame(test[,1],test[,2[i],test[,2[i]+1]) write.table(qq,file="C:\\Alice\\bb.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") i <- i+1; } But it's not right. I tried hard to revise it but I just can't make it. If someone can help me out here, your help is really greatly appreciated. Thank you sooo much! Best, Alice --------------------------------- [[alternative HTML version deleted]]
Gabor Grothendieck
2005-Nov-11 00:10 UTC
[R] How to export multiple files using write.table in the loop?
Try this: col.list <- list(1:3, c(1,4:5)) for(cols in col.list) write.table(data1[,cols], ...whatever...) On 11/10/05, alice.0309 <alice.0309 at yahoo.com.tw> wrote:> Hi, > > I tried to split a big file into some small files seperately by R. I can only do that writing duplicated codes. When I tried to write a loop, I only got one appned or destroyed exported file. For example: > > data1<- read.table(file = "C:\\Alice\\MBEI.txt", sep="\t", check.names=FALSE) > a<-subset(data1,select=c(V1,V2,V3)) > write.table<-(a,file="C:\\Alice\\aa1.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") > a<-subset(data1,select=c(V1,V4,V5)) > write.table(a,file="C:\\Alice\\aa2.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") > > or > data1<- read.table(file = "C:\\Alice\\MBEI.txt", sep="\t", check.names=FALSE) > a<-data.frame(data1[,1],data1[,2],data1[,3] > write.table<-(a,file="C:\\Alice\\aa1.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") > a<-data.frame(data1[,1],data1[,4],data1[,5] > write.table(a,file="C:\\Alice\\aa2.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") > > I tried to write a loop for it like : > > > i <- 1 > while (i <3) > { > qq<-data.frame(test[,1],test[,2[i],test[,2[i]+1]) > write.table(qq,file="C:\\Alice\\bb.txt",quote=FALSE,row.names=FALSE,col.name=FALSE,sep="\t") > i <- i+1; > } > > But it's not right. I tried hard to revise it but I just can't make it. If someone can help me out here, your help is really greatly appreciated. Thank you sooo much! > > Best, > > Alice > > > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Possibly Parallel Threads
- mydataframe$colname: using substring of colname may also match some column (PR#7496)
- Different behavior of model.matrix between R 3.2 and R3.1.1
- Different behavior of model.matrix between R 3.2 and R3.1.1
- Different behavior of model.matrix between R 3.2 and R3.1.1
- Call for testing: OpenSSH 7.2