Sometime an object has attribute names for identification of the value with names instead of index. There are time you need to extract the names of a vector or object. The easiest way is to do:

library(data.table)
dt <- cars[1,]
setDT(dt)
str(dt)
dt[, dist := as.character(dist)]

The above is just to make a toy data and convert column dist to character. In case we want to know later which column or names that we have converted to class `character`.

bb <- dt[, lapply(.SD, class)]

> names(bb)[bb == "character"]
[1] "dist"

This can then be implemented programmatically. Of course you can do str(bb) but that is not the point here and str() is only suitable for viewing purposes.

names 
comments powered by Disqus