[R] Loop Help

David L Carlson dcarlson at tamu.edu
Mon May 21 19:29:33 CEST 2012


Here is a slightly different approach that takes advantage of recycling:

# Make 7 data frames
for (i in 1:7) {
  assign(paste("TOWER", i, sep=""), data.frame(A=letters[1:4],
    X=rnorm(4)))
}
# Add Tower column taking advantage of recyling
tnames <- paste("TOWER", 1:7, sep="")
for (i in 1:7) {
  assign(tnames[i], cbind(eval(as.name(tnames[i])), Tower=i))
}
# Combine them into a single data frame
TOWER <- do.call(rbind, lapply(tnames, as.name))


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rui Barradas
> Sent: Saturday, May 19, 2012 9:04 AM
> To: bdossman
> Cc: r-help at r-project.org
> Subject: Re: [R] Loop Help
> 
> Hello,
> 
> The error is that you are trying to use the number of rows of a
> character string.
> TOWERS[1], TOWERS[2], etc, are not data frames. Use a print statement
> before the line that throws the error to check it.
> 
> Your problem can be solved along the lines of what follows.
> Note that I've put all data frames in a list, it's better to have them
> kept like that, it makes everything else simpler.
> 
> 
> # Create a list with some made up data.frames
> towers <- list(TOWER1=data.frame(A=letters[1:4], X=rnorm(4)),
>      TOWER2=data.frame(A=LETTERS[1:6], X=runif(6)))
> towers
> 
> # In your case this is  from TOWER1 to TOWER7
> TOWERS <- names(towers)
> 
> towers.with.id <- lapply(TOWERS, function(i){
>          towers[[ i ]]$Tower <- factor(i)
>          towers[[ i ]]})
> 
> 
> (Use something other than 'towers.with.id', this is just an example.)
> 
> 
> #names(towers.with.id) <- TOWERS  # (*) See below
> towers.with.id
> do.call(rbind, towers.with.id)
> 
> 
> (*) Try to run these last three instructions with the marked line
> commented/uncommented.
> It's better to uncomment, maybe after rbind.
> You'll later be able to access the list elements with a syntax like
> 
> 
> towers.with.id[[ "TOWER2" ]]        # full data.frame 2
> towers.with.id[[ TOWERS[2] ]]$A    # just that column
> towers.with.id[[ "TOWER2" ]]$A[3]    # third element of that column
> 
> 
> If you do.call/rbind before, to solve the rbind-ed data.frame's row
> names use
> 
> rownames(result) <- seq.int(nrow(result))
> 
> where 'result' is the result of do.call.
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 19-05-2012 11:00, r-help-request at r-project.org escreveu:
> > Date: Fri, 18 May 2012 16:14:08 -0700 (PDT)
> > From: bdossman<bdossman at gmail.com>
> > To:r-help at r-project.org
> > Subject: [R] Loop Help
> > Message-ID:<1337382848213-4630555.post at n4.nabble.com>
> > Content-Type: text/plain; charset=us-ascii
> >
> > Hi all,
> >
> > I am a beginner R user and need some help with a simple loop
> function.
> >
> > Currently, I have seven datasets (TOWER1,TOWER2...TOWER7) that are
> all in
> > the same format (same # of col and headers). I am trying to add a new
> column
> > (factor) to each dataset that simply identifies the dataset.
> Ultimately, I
> > would like to merge all 7 datasets and need that column to identify
> what
> > rows came from what dataset.
> >
> > Using the code below, I get the error message "Error in rep(i,
> > nrow(TOWER.i)) : invalid 'times' argument" but it doesn't make sense
> to me
> > since nrow should give an integer value. Any help will be really
> > appreciated.
> >
> > TOWERS<-
> c("TOWER1","TOWER2","TOWER3","TOWER4","TOWER5","TOWER6","TOWER7")
> >
> > for(i in 1:7){
> > 	TOWER.i<-TOWERS[i]
> > 	TOWER<-rep(i,nrow(TOWER.i))
> > 	TOWER.i<-cbind(TOWER.i[1:2],TOWER, TOWER.i[2:length(TOWER.i)])
> > }
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list