[R] recalling different data frames (the way you do in Excel VB but now) in

Ista Zahn izahn at psych.rochester.edu
Mon Mar 21 17:38:29 CET 2011


Hi Bodnar,
The "R way" is to put the data frames in a list:

id <-c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3)
a <-c(3,1,3,3,1,3,3,3,3,1,3,2,1,2,1,3,3,2,1,1,1,3,1,3,3,3,2,1,1,3)
b <-c(3,2,1,1,1,1,1,1,1,1,1,2,1,3,2,1,1,1,2,1,3,1,2,2,1,3,3,2,3,2)
c <-c(1,3,2,3,2,1,2,3,3,2,2,3,1,2,3,3,3,1,1,2,3,3,1,2,2,3,2,2,3,2)
d <-c(3,3,3,1,3,2,2,1,2,3,2,2,2,1,3,1,2,2,3,2,3,2,3,2,1,1,1,1,1,2)
e <-c(2,3,1,2,1,2,3,3,1,1,2,1,1,3,3,2,1,1,3,3,2,2,3,3,3,2,3,2,1,4)
dat <-data.frame(id,a,b,c,d,e)

and operate recursively on the list:

dat.list <- split(dat, dat$id)
for(i in names(dat.list)){
    dat.list[i] #do this and that
}

or

lapply(dat.list, function.that.does.this.and.that)

Best,
Ista
On Mon, Mar 21, 2011 at 12:05 PM, Bodnar Laszlo EB_HU
<Laszlo.Bodnar at erstebank.hu> wrote:
> Hello everyone,
>
> I'd like to ask you a question again, basically focusing on referring to different objects.
>
> Let's suppose we create the following databases this way:
>
> id <-c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3)
> a <-c(3,1,3,3,1,3,3,3,3,1,3,2,1,2,1,3,3,2,1,1,1,3,1,3,3,3,2,1,1,3)
> b <-c(3,2,1,1,1,1,1,1,1,1,1,2,1,3,2,1,1,1,2,1,3,1,2,2,1,3,3,2,3,2)
> c <-c(1,3,2,3,2,1,2,3,3,2,2,3,1,2,3,3,3,1,1,2,3,3,1,2,2,3,2,2,3,2)
> d <-c(3,3,3,1,3,2,2,1,2,3,2,2,2,1,3,1,2,2,3,2,3,2,3,2,1,1,1,1,1,2)
> e <-c(2,3,1,2,1,2,3,3,1,1,2,1,1,3,3,2,1,1,3,3,2,2,3,3,3,2,3,2,1,4)
> df <-data.frame(id,a,b,c,d,e)
> df
>
> for (i in 1:3) assign(paste("df", i, sep="."), split(df,df$id)[[i]])
> df.1
> df.2
> df.3
>
> Now in the next step I'd like to get or "recall" these databases but not just simply sending 'df.1', 'df.2', 'df.3' to R (because my real df database is much bigger and much more complicated than this simplified one as usual). I would have liked to do this the similar way you recall these things in Excel Visual Basic.
>
> You know, if we were in an Excel VB world I would do something like:
> sub exercise ()
> for i = 1 to 3
>                df.i
>                // perform this and that etc.//
>        next i
> end sub
>
> Returning to R: first I wanted to do it this way:
> for (i in 1:3)
>    df.i (perform this and that etc...)
>
> But of course it is wrong. Is there a proper way to handle this one? What do I miss? I do not know if my question is clear...
> Thank you very much and thanks for the previous answers as well!
> Happy R-exploring!
> Laszlo
>
> ____________________________________________________________________________________________________
> Ez az e-mail és az összes hozzá tartozó csatolt melléklet titkos és/vagy jogilag, szakmailag vagy más módon védett információt tartalmazhat. Amennyiben nem Ön a levél címzettje akkor a levél tartalmának közlése, reprodukálása, másolása, vagy egyéb más úton történő terjesztése, felhasználása szigorúan tilos. Amennyiben tévedésből kapta meg ezt az üzenetet kérjük azonnal értesítse az üzenet küldőjét. Az Erste Bank Hungary Zrt. (EBH) nem vállal felelősséget az információ teljes és pontos - címzett(ek)hez történő - eljuttatásáért, valamint semmilyen késésért, kapcsolat megszakadásból eredő hibáért, vagy az információ felhasználásából vagy annak megbízhatatlanságából eredő kárért.
>
> Az üzenetek EBH-n kívüli küldője vagy címzettje tudomásul veszi és hozzájárul, hogy az üzenetekhez más banki alkalmazott is hozzáférhet az EBH folytonos munkamenetének biztosítása érdekében.
>
>
> This e-mail and any attached files are confidential and/...{{dropped:19}}
>
>
> ______________________________________________
> 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.
>
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list