简体   繁体   English

使用 unclass() function 的目的是什么? 以及为什么出现错误“”列索引必须最多为 2,而不是 3、4、5、6、7、8、9、10”

[英]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?在r中使用unclass() function的目的是什么? I can't get it right.我做错了。
Can you demisifiy it with this code below?你能用下面的代码对它进行 demisifiy 吗?

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. 因为上面的语句返回了数据帧,并且因为我们将数据帧返回了变量,所以它将引发错误“列索引必须为2,如果为正值,而不是3,4,5,6,7,8,9,10 ,因此我们需要对其进行取消分类,以将数据框转换为列表。

and since the unclass return list so we need [[2]] to access the value of returned list 并且由于取消类返回列表,所以我们需要[[2]]访问返回列表的值

Let me try explaining the usage of 'unclass' 让我尝试解释“ 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应用于这组因子,则unclass会将因子转换为其数量,例如:

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 Unclass()就像 pandas 中的 label 编码。它只是将分类数据从 1 到 n 排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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