r - Why do columns in data frames change class when subsetted versus apply? -


this question has answer here:

i trying add summary row data frame detailing levels of each column. ran problem applying levels function across frame. think reason columns treated individually treated factor vectors, when apply function used treated characters:

a = c("a","b","c") b = c("d","e","f") m = cbind(a,b) df = as.data.frame(m) class(df[,1]) [1] "factor" apply(df, margin=2, class)                     b  "character" "character"  

which think cause of problem:

levels(df[,1]) [1] "a" "b" "c" apply(df, margin=2, levels) null 

i had @ documentation on apply, data frames, , around web. can explain why is?

you can use lapply or sapply function know class of variables, understanding apply goes through column element wise each element character output shows as character, lapply , sapply functions works on variables gives class of variables either character or factor

lapply(df,class) $a [1] "factor"  $b [1] "factor"  sapply(df,class)               b  "factor" "factor"  

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -