[R] simple usage of "for"

Hans Ekbrand hans.ekbrand at sociology.gu.se
Tue Feb 19 15:24:16 CET 2008


Hi list

I have a data frame I would like to loop over. To begin with I would
like crosstabulations using the first variabel in the data frame,
which is called "meriter".

> table(meriter[[1]], meriter[[3]])
                                                      
                                                          ja nej
  Annan                                                 0  2   1
  Avdelningen för teknik- och vetenskapsstudier         0  5   1
  CEFOS                                                 0  6   3
  Förvaltningshögskolan                                 0 13   6
  Institutionen för globala studier                     0 20  12
  Institutionen för journalistik och masskommunikation  0  5  17
  Institutionen för socialt arbete                      1 19  35
  Psykologiska institutionen                            0 24  21
  Sociologiska institutionen                            0 16  12
  Statsvetenskapliga institutionen                      0 19  12
>

I tried the following small code snippet which I copied from the
"Introduction to R":

> for (i in 2:length(meriter)) { table(meriter[[1]], meriter[[i]]) }
> 

And there is no output at all, just a new prompt.

I added a print statement just to check the loop construct, and it
seems to work.

> for (i in 2:length(meriter)) { print(i); table(meriter[[1]], meriter[[i]]) }
[1] 2
[1] 3
[1] 4

But I get no tables :-(

What do I do wrong?

-- 
Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net>
GPG Fingerprint: 1408 C8D5 1E7D 4C9C C27E 014F 7C2C 872A 7050 614E
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: Digital signature
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080219/871ff4ed/attachment.bin 


More information about the R-help mailing list