[R] how to write a loop to repetitive jobs

Albrecht Kauffmann @|k@u||m @end|ng |rom |@@tm@||@|m
Tue Apr 17 08:15:40 CEST 2018


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

-- 
  Albrecht Kauffmann
  alkauffm using fastmail.fm

Am Mo, 16. Apr 2018, um 22:54, schrieb Ding, Yuan Chun:
> Hi All..,
> 
> I need to do the following repetitive jobs:
> 
> seg71 <- read.csv("C:/Awork/geneAssociation/removed8samples/neuhausen71/
> seg.pr3.csv", head=T)
> seg71$id <-"sn71"
> 
> seg72 <- read.csv("C:/Awork/geneAssociation/removed8samples/neuhausen72/
> seg.pr3.csv", head=T)
> seg72$id <-"sn72"
> 
> seg73 <- read.csv("C:/Awork/geneAssociation/removed8samples/neuhausen73/
> seg.pr3.csv", head=T)
> seg73$id <-"sn73"
> 
> seg74 <- read.csv("C:/Awork/geneAssociation/removed8samples/neuhausen74/
> seg.pr3.csv", head=T)
> seg74$id <-"sn74"
> 
> seg75 <- read.csv("C:/Awork/geneAssociation/removed8samples/neuhausen75/
> seg.pr3.csv", head=T)
> seg75$id <-"sn75"
> 
> seg <- rbind (seg71, seg72, seg73, seg74, seg75)
> 
> I want to write a loop to do it;
> 
> For ( d in 71:75) {
>   Dir<-paste("C:/Awork/geneAssociation/removed8samples/neuhausen", i, 
> sep="")
>   setwd(Dir)
> ..........
> then I do not know how to create objects seg71 to seg75;  in SAS, it 
> would be  seg&d    ;
> 
> I like R, but not good at R.
> 
> Can you help me?
> 
> Thank you,
> 
> Ding
> 
> 
> ---------------------------------------------------------------------
> -SECURITY/CONFIDENTIALITY WARNING-
> This message (and any attachments) are intended solely...{{dropped:8}}




More information about the R-help mailing list