So I have this problem with R. I have a table and I need to find what is the class of the variable (i.e. I'm presuming that would be referring to the data in the columns).
The data is quite big i.e. many rows.
Whenever I write class(nameofthedata)
it doesn't work and whenever I write class("titleofthecolumn")
it gives me back "character i.e. referring to the title of the column which is not what I'm after...
I would suggest the following solution:
# Load sample data
data(mtcars)
# Classes
sapply(mtcars, class)
You can the convinetly identify classes of all variables in the data frame:
>> t(t(sapply(mtcars, class)))
[,1]
mpg "numeric"
cyl "numeric"
disp "numeric"
hp "numeric"
drat "numeric"
wt "numeric"
qsec "numeric"
vs "numeric"
am "numeric"
gear "numeric"
carb "numeric"
t()
used only for the code presentation.
If you want to know the class of a specific column, say schoolid
, just call class()
on that column, like so:
df <- structure(list(schoolid = c(1L, 1L, 1L, 1L, 1L, 1L),
score = c(0L, 10L, 0L, 40L, 42L, 4L),
gender = c(1L, 1L, 1L, 1L, 1L, 1L)),
.Names = c("schoolid", "score", "gender"),
row.names = c(NA, 6L),
class = "data.frame")
class(df$schoolid)
# returns [1] "integer"
If you want to know the classes of all columns, you can apply class()
to all columns using sapply()
:
sapply(df, class)