Bodnar Laszlo EB_HU
2011-Mar-21 16:05 UTC
[R] recalling different data frames (the way you do in Excel VB but now) in
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}}
Ista Zahn
2011-Mar-21 16:38 UTC
[R] recalling different data frames (the way you do in Excel VB but now) in
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
Maybe Matching Threads
- recalling different data frames (the way you do in Excel VB)
- persuade tabulate function to count NAs in a data frame
- calculating the occurrences of distinct observations in the subsets of a dataframe
- changing the dimensions of a matrix in a real specific way
- creating additional column