Reputation: 3
I have a dataframe with large number of categorical variables. I need to create a list, where I would get the output similar to str(DF), but not truncated
Example:
Df <- data.frame(
x = as.factor(sample(1:5,30,r=T)),
y = as.factor(sample(1:5,30,r=T)),
z = as.factor(sample(1:5,30,r=T)),
w = as.factor(sample(1:5,30,r=T))
)
str(Df)
produces output like this:
x: Factor w/ 5 levels "1","2","3","4",..:
But this is insufficient for me, both because I need all levels, and because I have a large number of variables in the dataframe, so I also get the " [list output truncated]: message...
Ideal thing is to get what levels(Df$x) does for each variable. I hoped for this code to work:
attach(Df)
idnames <- names(Df)
levellist <- lapply(idnames, function(x) levels(x))
##
but it returns NULL for all variables..
Upvotes: 0
Views: 1734
Reputation: 3905
levels
works on factors, so use it on Df
, which is a list of factors.
levellist <- lapply(Df, function(x) levels(x))
Upvotes: 1