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 f...{{dropped:22}}
Hello,
The following might do it. Without data it's untested.
wd <- function(i){
paste0("C:/Awork/geneAssociation/removed8samples/neuhausen7", i,
"/seg.pr3.csv")
}
seg <- lapply(1:5, function(i) {
DF <-read.csv(wd(i))
DF$id <- paste0("sn7", i)
DF
})
seg <- do.call(rbind, seg)
row.names(seg) <- NULL
Hope this helps,
Rui Barradas
On 4/16/2018 9:54 PM, Ding, Yuan Chun wrote:> 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 f...{{dropped:22}}
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
Hi Rui,
Thank you very much!! It worked very well, I am looking into how to use lapply
and do.call.
Ding
-----Original Message-----
From: Rui Barradas [mailto:ruipbarradas at sapo.pt]
Sent: Monday, April 16, 2018 2:16 PM
To: Ding, Yuan Chun; r-help at r-project.org
Subject: Re: [R] how to write a loop to repetitive jobs
Hello,
The following might do it. Without data it's untested.
wd <- function(i){
paste0("C:/Awork/geneAssociation/removed8samples/neuhausen7", i,
"/seg.pr3.csv")
}
seg <- lapply(1:5, function(i) {
DF <-read.csv(wd(i))
DF$id <- paste0("sn7", i)
DF
})
seg <- do.call(rbind, seg)
row.names(seg) <- NULL
Hope this helps,
Rui Barradas
On 4/16/2018 9:54 PM, Ding, Yuan Chun wrote:> 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
> f...{{dropped:22}}
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
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 at 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}}
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
>
Try this:
result <- lapply(71:75, function(x){
# use 'paste0' to add the number to the file name
input <-
read.csv(paste0("C:/Awork/geneAssociation/removed8samples/neuhausen",
x,
"/seg.pr3.csv")
, head=TRUE
)
input$id <- paste0("sn", x)
input # return the input
})
result <- do.call(rbind, result) # combine dataframes together
?
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Mon, Apr 16, 2018 at 1:54 PM, Ding, Yuan Chun <ycding at coh.org>
wrote:
> 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:13}}