Is this what you want?

mycol.index<-c(1,5,3)
apply(mydata[,mycol.index],2, function(x)nlevels(factor(x)))

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

On 06/08/07, Sébastien <[EMAIL PROTECTED]> wrote:
>
> Dear R users,
>
> I am currently trying to create my first personnal function and use it
> with the apply function. The purpose of this function is to create a
> vector summarizing the number of levels in a given selection of
> data.frame columns.
> I tried to transpose the indexation method used by the nlevels function
> but it doesn't seem to work. I did not find anything uesful in the
> archives so could someone point to me where my mistake(s) is (are) ?
>
> Thanks in advance
>
> Sebastien
>
> #-------------------------
>
> mydata<-data.frame
> (1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6)
> mycol.index<-c(1,5,3)
>
> nelem<-function(x,col.id) nlevels(factor(x[,col.id]))
> my.nlevels.col<-apply(mydata,2,nelem,mycol.index)
> my.nlevels.col
>
> #----------------------------
>
> #The error message is the following
> #>Error in x[, col.id] : incorrect number of dimensions
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch 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.

Reply via email to