Hi,

On Fri, Apr 30, 2010 at 12:08 PM, Jeff Brown <dopethatwantsc...@yahoo.com>wrote:

>
> Hi,
>
> I still have little ability to predict how these functions will treat the
> columns of data frames:
>
> > # Here's a data frame with a column "a" of integers,
> > # and a column "b" of characters:
> > df <- data.frame(
> + a = 1:2,
> + b = c("a","b")
> + )
> > df
>  a b
> 1 1 a
> 2 2 b
> >
> > # Except -- both columns are characters:
> > apply (df, 2, typeof)
>          a           b
> "character" "character"
>

apply converts all to character


> >
> > # Except -- they're both integers:
> > lapply (df, typeof)
> $a
> [1] "integer"
>
> $b
> [1] "integer"
>
>
data.frame has a argument 'stringsAsFactors', this converts character
columns to factor columns.
Factors are integers with labels


> >
> > # Except -- only one of those integers is numeric:
> > lapply (df, is.numeric)
> $a
> [1] TRUE
>
> $b
> [1] FALSE
>

df$b is a factor.

>
>
> Many thanks,
> Jeff
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Why-do-data-frame-column-types-vary-across-apply-lapply-tp2077054p2077054.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org 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.
>



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

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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