[英]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
在r中使用unclass() function的目的是什么? 我做錯了。
你能用下面的代碼對它進行 demisifiy 嗎?
unclass(tele%>%mutate(dec=ntile(rev_Range,n=10))%>%count(dec)%>%unname())[[2]]
我得到了答案。
因為上面的語句返回了數據幀,並且因為我們將數據幀返回了變量,所以它將引發錯誤“列索引必須為2,如果為正值,而不是3,4,5,6,7,8,9,10 ,因此我們需要對其進行取消分類,以將數據框轉換為列表。
並且由於取消類返回列表,所以我們需要[[2]]訪問返回列表的值
讓我嘗試解釋“ unclass”的用法
假設您具有以下顏色向量:
cores = c('blue','green','red')
並將一組字符串存儲為因素,例如:
val = c('setosa','setosa','virginica','versicolor','virginica','setosa')
val_fac = factor(val)
如果您將unclass應用於這組因子,則unclass會將因子轉換為其數量,例如:
unclass(val_fac)
[1] 1 1 3 2 3 1
attr(,"levels")
[1] "setosa" "versicolor" "virginica"
使用這些數字,您可以通過執行以下操作將因子轉換為顏色:
cores[unclass(val_fac)]
[1] "blue" "blue" "red" "green" "red" "blue"
希望這對您有幫助,
最好的祝福,
古斯塔沃
Unclass()
就像 pandas 中的 label 編碼。它只是將分類數據從 1 到 n 排序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.