首页 > 解决方案 > 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

我应该如何使用空数据内联行?

标签: rnullcluster-analysis

解决方案


我认为使用 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

推荐阅读