R, class() returns different results for data frame columns -


> bb = data.frame(x = c( 11:13), y = c(1:3), z = c("a", "a", "b")) > bb    x y z 1 11 1 2 12 2 3 13 3 b >  > apply( bb, 2, class)           x           y           z  "character" "character" "character"  > > apply( bb[,1:2], 2, class)         x         y  "integer" "integer"  >     > apply( bb[,2:3], 2, class)           y           z  "character" "character"  >  > class(bb$z) [1] "factor" >  

i quite surprised find class() behavior illustrated above.could please give rationale above inconsistencies. many thanks.

the reason apply converts character class converts matrix , matrix can hold single class i.e. if there @ least 1 column non-numeric, entire dataset gets changed character. instead, use,

lapply(bb, class) #$x #[1] "integer"  #$y #[1] "integer"  #$z #[1] "factor" 

the above returns output in list. if need vector, use sapply

sapply(bb, class) 

or

vapply(bb, class, '') 

you can alternatively use str() inspect data frame

str(bb) # 'data.frame': 3 obs. of  3 variables: #  $ x: int  11 12 13 #  $ y: int  1 2 3 #  $ z: factor w/ 2 levels "a","b": 1 1 2 

as op showed in post whenever numeric columns, correct class displayed apply until non-numeric column added mix.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -