[R] how to write a loop to repetitive jobs

K. Elo m@||||@t@ @end|ng |rom pp@|net@||
Tue Apr 17 08:27:53 CEST 2018


Hi!

An alternative with 'assign':

for ( i in 71:75) {
  setwd(paste("C:/Awork/geneAssociation/removed8samples/neuhausen", i,
sep=""))
  temp.df<-read.csv("seg.pr3.csv", head=T)
  temp.df$id<-paste0("sn",i,sep="")
  assign(paste0("seg",i,sep=""),temp.df)
}
rm(temp.df,i)    # Clean up

HTH,
Kimmo


2018-04-17 kello 08:15 +0200, Albrecht Kauffmann wrote:
> Hello Ding,
> 
> try this:
> 
> seg <- list()
> for ( d in 71:75) {
>   s <- paste0("seg",d)
>   sn <- paste0("sn",d)
>   Dir<-paste("C:/Awork/geneAssociation/removed8samples/neuhausen", i,
> sep="")
>   setwd(Dir)
>   seg[[s]] <- read.csv("seg.pr3.csv", head=T)
>   seg[[s]]$id <- sn
> }
> 
> Greetings,
> Albrecht
>




More information about the R-help mailing list