[R] calling data frames

Liaw, Andy andy_liaw at merck.com
Tue May 11 15:06:00 CEST 2004


Not sure how "automated" you want, but the following might work (obviously
untested):

Guilda.list <- list(Guilda1 = c(27,48),
                    Guilda2 = c(5,17,19,20,21,24:26,40,41,77),
                    Guilda3 = c(22,28,69),
                    Guilda4 = c(29:37,78),
                    Guilda5 = c(3,8,18,23,63,82,83),
                    Guilda6 = c(6,38,39,44:62,84),
                    Guilda7 = c(1,2,42,43,64:68,79,80),
                    Guilda8 = c(7,15,16,70:76),
                    Guilda9 = c(4,9,10:14,81))
fnames<- list.files(pattern=".txt")
outfile <- "BLU_Var_%04d.txt"
for(i in seq(along=fnames)) {
    blumenau <- data.matrix(read.table(fnames[i],h=T)[2:50])
    Guild.Richness <- sapply(Guilda.list, function(idx) sum(Guildas[idx,]))
    write(Guild.Richness, file = sprintf(outfile, i), ncol = 9)
}

You can replace the for loop with lapply(), but that probably won't make
much difference...

HTH,
Andy

> From: Rogério Rosa da Silva
> 
> Dear List,
> 
> I've around 1000 *.txt files, I've generate with other software.
> I've now done the following code (below).
> My question is how can I automate this (with do.call () ?), 
> so it could be 
> done for all the  *.txt files.
> 
> Thanks in advance,
> Rogério
> 
> 
> names<- list.files()
> file <- "BLU_Var_%04d.txt"
> for(i in 1:1000){
> blumenau<-read.table("Blu_1.txt",h=T)   # 1000 *.txt files
> Guildas<-data.frame(cbind(t(blumenau[2:50])))
> Guilda1<-cbind(X27,X48)
> Guilda2<-cbind(X5,X17,X19,X20,X21,X24,X25,X26,X40,X41,X77)
> Guilda3<-cbind(X22,X28,X69)
> Guilda4<-cbind(X29,X30,X31,X32,X33,X34,X35,X36,X37,X78)
> Guilda5<-cbind(X3,X8,X18,X23,X63,X82,X83)
> Guilda6<-cbind(X6,X38,X39,X44,X45,X46,X47,X49,X50,X51,X52,X53,
> X54,X55,X56,X57,X58,X59,X60,X61,X62,X84)
> Guilda7<-cbind(X1,X2,X42,X43,X64,X65,X66,X67,X68,X79,X80)
> Guilda8<-cbind(X7,X15,X16,X70,X71,X72,X73,X74,X75,X76)
> Guilda9<-cbind(X4,X9,X10,X11,X12,X13,X14,X81)
> Resul.Guilda1<-sum(apply(Guilda1,2,sum))
> Resul.Guilda2<-sum(apply(Guilda2,2,sum))
> Resul.Guilda3<-sum(apply(Guilda3,2,sum))
> Resul.Guilda4<-sum(apply(Guilda4,2,sum))
> Resul.Guilda5<-sum(apply(Guilda5,2,sum))
> Resul.Guilda6<-sum(apply(Guilda6,2,sum))
> Resul.Guilda7<-sum(apply(Guilda7,2,sum))
> Resul.Guilda8<-sum(apply(Guilda8,2,sum))
> Resul.Guilda9<-sum(apply(Guilda9,2,sum))
> Guild.Richness<-cbind(Resul.Guilda1,Resul.Guilda2,Resul.Guilda
> 3,Resul.Guilda4,Resul.Guilda5,Resul.Guilda6,Resul.Guilda7,Resu
> l.Guilda8,Resul.Guilda9)
> write(Guild.Richness, file = sprintf(file, i), ncol = 9)
> }
> 
> 
> -- 
> Rogério R. Silva
> MZUSP http://www.mz.usp.br
> Linux User #354364
> Linux counter http://counter.li.org
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list