[R] For loop to cycle through datasets of differing lengths

Peter Langfelder peter.langfelder at gmail.com
Thu Nov 3 19:59:34 CET 2011

On Thu, Nov 3, 2011 at 11:41 AM, Schatzi <adele_thompson at cargill.com> wrote:
> I have encountered this problem on several occasions and am not sure how to
> handle it. I use for-loops to cycle through datasets. When each dataset is
> of equal length, it works fine as I can combine the datasets and have each
> loop pick up a different column, but when the datasets are differing
> lengths, I am struggling. Here is an example:
> A<-1:10
> B<-1:15
> C<-1:18
> Set1<-data.frame(A,runif(10))
> Set2<-data.frame(B,runif(15))
> Set3<-data.frame(C,runif(18))
> for (i in 1:3){
> if (i==1) Data<-Set1 else if (i==2) Data<-Set2 else Data<-Set3
> dev.new()
> plot(Data[,1],Data[,2])
> }
> I don't always want to plot them and instead do other things, such as fit a
> non-linear equation to the dataset, etc. I end up using that "if" statement
> to cycle through the datasets and was hoping there is an easier method.
> Maybe one would be to add extra zeros until they are the same length and
> then take out the extra zeros in the first step. Any help would be
> appreciated.

Well, you can start by putting your data sets in a list:

allSets = list(Set1, Set2, Set3)

then in your loop say

Data = allSets[[i]]

and you're done.

If you have an apriori unknown number of data sets, you can use get.
Before the loop you define the names,

setNames = c("Set1", "Set2", "Set3")

Then in the loop, you can use

Data = get(setNames[i])

But I'm not sure why this would be more useful than the list example
unless perhaps if you `load()' previously `save()'d data sets (the
load() function can give you the names of loaded variables). In any
case, if you repeat the same analysis on a number of data sets with
different dimensions, list() is IMHO the way to go.



More information about the R-help mailing list