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"]))) } Thank you in advance, Fabrizio --------------------------------------------------------------- Fabrizio De Amicis IT Department Generali Information Technologies - (GIT) Centro Galleria 2, Via Cantonale CH - 6928 Manno - Switzerland Tel +41 91 806 6220 Fax +41 91 806 6298 E-mail: fabrizio.deamicis at git.generali.ch ************************************************************************ The information in this email is confidential and may be legally... {{dropped}}
Hi, 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).> > }-- Cheers, Kevin ------------------------------------------------------------------------------ /* 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)
Dear Fabrizio At 12:07 PM 4/29/2003 +0200, 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"]))) > >} >The short answer is that you need to paste the number in i to "V" in order to create a proper column index. I assume that the columns of dt are named "V1", ..., "V15": print(dim(table(dt[,paste("V",i,sep="")]))) Alternatively, you could use something like for (i in paste("V",1:15,sep="")) print(dim(table(dt[,i]))) or even more simply, just index the columns by number for (i in 1:15) print(dim(table(x[,i]))) Some other points, however: (1) The argument to your function is x, but you're indexing a variable called dt. (2) A simpler approach to counting the unique elements in each column is apply(dt, 2, function(x) length(unique(x))) (3) Since this apparently applies to a particular matrix or data frame, why write a function to perform the operation? I hope that this helps, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox