简体   繁体   中英

What is the purpose of using unclass() function? and why the error ""the column indexes must be at most 2 if postive,not 3,4,5,6,7,8,9,10" appears

What is the purpose of using unclass() function in r? I can't get it right.
Can you demisifiy it with this code below?

unclass(tele%>%mutate(dec=ntile(rev_Range,n=10))%>%count(dec)%>%unname())[[2]]

I got the answer.

Because the statement above return dataframe, and because we return a data frame to variable, it will throw the error "the column indexes must be at most 2 if postive,not 3,4,5,6,7,8,9,10,so we need to unclass it to convert dataframe to list.

and since the unclass return list so we need [[2]] to access the value of returned list

Let me try explaining the usage of 'unclass'

Let's suppose you have this vector of colors:

cores = c('blue','green','red')

And a group of strings stored as factors, for example:

val = c('setosa','setosa','virginica','versicolor','virginica','setosa')

val_fac = factor(val)

If you apply unclass to this group of factors, unclass will convert the factors to their numbers, like:

unclass(val_fac)

[1] 1 1 3 2 3 1
attr(,"levels")
[1] "setosa"     "versicolor" "virginica" 

With these numbers you can convert the factors to colors, by doing:

cores[unclass(val_fac)]

[1] "blue"  "blue"  "red"   "green" "red"   "blue" 

Hope this helps you,

Best regards,

Gustavo,

Unclass() is like label encoding in pandas. It just orders categorical data from 1 to n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM