[R] dataframe columns class

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Dec 18 18:38:09 CET 2016


The apply function operates on arrays, so your data frame is being converted to an array (matrix) before doing its thing.  So use lapply or one of its variants. 
-- 
Sent from my phone. Please excuse my brevity.

On December 18, 2016 9:07:52 AM PST, "Cleber N.Borges via R-help" <r-help at r-project.org> wrote:
>Why columns classes are function dependents?
>Like this example:
>
> > for( i in 1:5 ) print( class( iris[,i] ) )
>[1] "numeric"
>[1] "numeric"
>[1] "numeric"
>[1] "numeric"
>[1] "factor"
> >
> > apply( iris, 2, class )
>Sepal.Length  Sepal.Width Petal.Length  Petal.Width Species
>  "character"  "character"  "character"  "character" "character"
> >
>
>
>
> > dat <- data.frame( v1=1:5, v2=letters[1:5] )
> > dat
>   v1 v2
>1  1  a
>2  2  b
>3  3  c
>4  4  d
>5  5  e
> >
> > summary( dat )
>        v1    v2
>  Min.   :1   a:1
>  1st Qu.:2   b:1
>  Median :3   c:1
>  Mean   :3   d:1
>  3rd Qu.:4   e:1
>  Max.   :5
> >
> > apply( dat, 2, class )
>          v1          v2
>"character" "character"
> >
>
>
>---
>Este email foi escaneado pelo Avast antivírus.
>https://www.avast.com/antivirus
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.



More information about the R-help mailing list