Re: [R] for loop, my program does not make a cycle

From: jim holtman <jholtman_at_gmail.com>
Date: Wed, 14 May 2008 12:53:47 -0400

You got exactly what you were asking for: the value the last time through the loop. If you are trying to capture all 11 values, then you might consider a list:

d<-as.vector(levels(a2$combin2))
a1 <- b1 <- vector('list',11)
for (i in 1:11){
a1[[i]] <-a2[a2$combin2%in%d[i],]
b1[[i]]<-b0[b0$Date%in%(a1[[i]]$Date),]
}

or using 'lapply'

result <- lapply(1:11, function(.index){

    a1 <- a2[a2$combin2 %in% d[.index],]     list(a1=a1, b1=b0[b0$Date %in% a1$Date, ] })

'result' is a list with both values in it for each iteration.

On Wed, May 14, 2008 at 11:13 AM, J S <yulya258_at_hotmail.com> wrote:

>
> Dear R community,
>
> I wrote a small program using for loop but it does not make cycles.
>
> My data: Dataframes: a2, a1, b0 and b1. Vector: d
>
> I would like to get b1 for each of i., i.e. totally 11. However, the
> program gives me b1 only for the last i =11.
>
> d<-as.vector(levels(a2$combin2))
> for (i in 1:11){
> a1<-a2[a2$combin2%in%d[i],]
> b1<-b0[b0$Date%in%(a1$Date),]
> }
>
> Your help is appreciated. Maybe someone could also recommend me good
> literature on for loops in R?
>
> Thanks,
> Julia
> _________________________________________________________________
> Get Free (PRODUCT) REDô Emoticons, Winks and Display Pics.
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]


______________________________________________ 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.

Received on Wed 14 May 2008 - 20:28:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 14 May 2008 - 20:30:49 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive