[R] Loop Help
Simon Knapp
sleepingwell at gmail.com
Sat May 19 13:06:30 CEST 2012
First, what was going wrong:
#this creates a character vector, not an object containing your data sets
TOWERS <- c("TOWER1","TOWER2","TOWER3","TOWER4","TOWER5","TOWER6","TOWER7")
for(i in 1:7){
# this creates a variable named "TOWER.i" (not "TOWER.1, TOWER.2,
..., TOWER.7)
# which will get overwritten each time through the loop
TOWER.i <- TOWERS[i]
# This is what causes your error, as TOWER.i is a string
("TOWER1", "TOWER2", ...), and hence nrow(TOWER.i) return NULL
TOWER <- rep(i,nrow(TOWER.i))
# if you got to here, you would have got more errors as TOWER.i is
cannot be subsetted
# (it is a string, not a 'dataset' with columns)
TOWER.i <- cbind(TOWER.i[1:2], TOWER, TOWER.i[2:length(TOWER.i)])
}
# Note that to create variables like 'TOWER.1', ..., you would have to
say something like
assign(paste('TOWER', i, sep='.'), TOWERS[[i]])
# inside the loop. However, this is not necessary here and you would
keep needing to
# access the variable with similar ugliness. Here is the way I might
approach this:
TOWERS <- list(TOWER1,TOWER2,TOWER3,TOWER4,TOWER5,TOWER6,TOWER7)
# create a list of things that look like what you wanted TOWERS.i to look like
# (still with column two repeated
TOWERS.modified <- mapply(function(x, i) cbind(x[,1:2], i,
x[,2:ncol(x)]), TOWERS, 1:length(TOWERS), SIMPLIFY=F)
# you can then 'merge' them (though that term would mean something else to most
# R programmers - see ?merge) with:
TOWERS.combined <- do.call(rbind, TOWERS.modified)
#Now what I would actually do is:
TOWERS.combined <- do.call(rbind, lapply(1:7, function(i) {
x <- get(paste('TOWER', i, sep=''))
cbind(x[,1:2], i, x[,2:ncol(x)])
}))
Hopefully that makes sense, if not I can provide more explanation, but
please try ?do.call, ?get, ?lapply and ?mapply first
Simon
On Sat, May 19, 2012 at 9:14 AM, bdossman <bdossman at gmail.com> wrote:
> 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)])
> }
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Loop-Help-tp4630555.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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