2

This is a short question, but how do I look up the dimensions of a factor variable in R? I tried:

dim(X)
nrow(X)
ncol(X)

but all return NULL

Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • 1
    You should reassign the word "dimensions" in your R-brain convolutions or gyri to describe `matrices` and `data.frames`. In R these are NOT "dimensions". – IRTFM Feb 07 '13 at 04:30

2 Answers2

6

If we have an atomic variable (which a factor is), it will have a length value

X <- factor(letters)

length(X)

## [1] 26

It will also have levels

levels(x)

When X is a factor, it is the following classes

is(X)
[1] "factor"   "integer"  "oldClass" "numeric"  "vector"  
mnel
  • 113,303
  • 27
  • 265
  • 254
1

Also the str() function is helpful when one has a dataframe and needs to get an idea of many different levels at once.

Jack Ryan
  • 2,134
  • 18
  • 26