[R] easy question

Ko-Kang Kevin Wang kwan022 at stat.auckland.ac.nz
Tue Apr 29 12:51:57 CEST 2003


On Tue, 29 Apr 2003, De Amicis Fabrizio (G.I.T.) wrote:

> Dear R.List,
> I am starting to use R. I have an easy question. In a dataset of 15
> variables, I am not able run correctly the index i of the do loop. 
> Do you have any suggestion? 
> NC <- function(x)
>     for (i in 1:15) {  
>     print(dim(table(dt[,"Vi"])))

Why do you have "Vi" with quotes?  Your counter i isn't used anywhere in 
the loop.  I am assuming your dataset of 15 variables is a data frame or 
matrix?  Then you'll need dt[, i] instead.  It will read the $i^{th}$ 
column each time it runs through the loop.

On a side note.  The above function isn't right.  The NC function will 
need a variable x, which isn't used anywhere in the function.  You don't 
need to put the for() loop into the function at all.  (I'm assuming you 
have got a data frame or matrix called dt).

> }



/* Time is the greatest teacher, unfortunately it kills its students */

Ko-Kang Kevin Wang
Master of Science (MSc) Student
SLC Tutor and Lab Demonstrator
Department of Statistics
University of Auckland
New Zealand
Homepage: http://www.stat.auckland.ac.nz/~kwan022
Ph: 373-7599
    x88475 (City)
    x88480 (Tamaki)

More information about the R-help mailing list