r - x[[jj]][iseq] <- vjj 中的错误:替换在 R 中的长度为零(KlaR 包)
问题描述
我有一个包含 188 列和 100 行(加上标题行)的数据集。我正在尝试将 R 中的kmodes
聚类方法(来自klaR
包)应用于该矩阵。
数组数据结构中有两种类型的数据:字符串和二进制。两者都有空值。
例如:
Q27_history Q28
1 <NA>
<NA> yes, sometimes
计算簇内总平方和的函数:
set.seed (96743)
# function to compute total within-cluster sum of square
wss <- function(k) {
sum((kmodes( whois_data, k)$withindiff))
}
# Compute and plot wss for k = 1 to k = 15
k.values <- 2:15
# extract wss for 2-15 clusters
wss_values <- map_dbl(k.values, wss)
print(wss_values)
错误文本:
Error in x[[jj]][iseq] <- vjj : replacement has length zero
担心:
Error in print(wss_values) :object 'wss_values' is not found
我试图kmodes(na.fill(data, fill=""), k)
输入:
wss <- function(k) {
sum((kmodes( whois_data, k)$withindiff))
kmodes(na.fill(data, fill=""), k)
}
但在那之后library(purrr)
停止工作并且没有找到变量map_dbl
我应该如何使用空数据内联行?
解决方案
我认为使用 kmodes 时不能使用 NA,它应该会引发错误:
set.seed(111)
whois_data = data.frame(Q1 = rbinom(100,1,0.5),
Q2 = sample(c("Y","N"),100,replace=TRUE),
Q3 = sample(c(NA,1:3),100,replace=TRUE))
kmodes(whois_data,3)
Error in old.cluster != cluster :
comparison of these types is not implemented
在没有 NA 的情况下做 kmmodes 更有意义:
wss <- function(k,df) {
sum((kmodes(df, k)$withindiff))
}
library(purrr)
map_dbl(2:5, wss,df = whois_data[complete.cases(whois_data),])
[1] 91 58 70 42
推荐阅读
- r - 嵌套列表的列均值
- sql - 此 sql 命令中缺少什么表达式?
- java - 有没有办法使用 cdk 创建没有 CA(多帐户注册)的证书?
- javascript - 平面列表未显示每个对象元素
- c# - 如何在 ListView x:name 内的 Views 中访问 x:name?是否可以设置不同形式的用户输入?
- google-apps-script - 如何从 Google Apps 脚本的最后一行提取值
- react-native - 我可以将我的广告横幅固定在底部导航旁边的屏幕底部吗?
- python - 从未排序的链表中删除重复项(python)
- c# - 带有用于输入的虚线的文本框
- algorithm - 设计一个 URL 回调调度器