'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]]
Solution 1:[1]
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,
Solution 2:[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
Solution 3:[3]
Unclass()
is like label encoding in pandas. It just orders categorical data from 1 to n
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gustavo Mirapalheta |
Solution 2 | Ehsno |
Solution 3 | patL |