简体   繁体   English

遍历列表时出错:“ [[-。data.frame`(`* tmp *`,,i,value = c(7L,1L,4L,7L,7L,:新列将留下孔……”)中的错误。

[英]Error looping through list: “Error in `[<-.data.frame`(`*tmp*`, , i, value = c(7L, 1L, 4L, 7L, 7L, : new columns would leave holes… ”

I'm trying to write a function that loops through a list in order to run kmeans clustering on only specific columns of a dataset. 我试图编写一个遍历列表的函数,以便仅在数据集的特定列上运行kmeans聚类。 I want the output to be a matrix/dataframe of the cluster membership of each observation when kmeans is run on each set of columns. 当kmeans在每组列上运行时,我希望输出为每个观察值的群集成员的矩阵/数据框。

Here's a mock dataset and the function I came up with (I'm new to R--sorry if it's shaky) 这是一个模拟数据集和我想出的功能(我是R的新手,如果太不稳定,很抱歉)

set.seed(123)
mydata <- data.frame(a = rnorm(100,0,1), b = rnorm(100,0,1), c = 
rnorm(100,0,1), d = rnorm(100,0,1), e = rnorm(100,0,1)) 

set.seed(123)
my.kmeans <- function(data,k,...) {
    clusters <- data.frame(matrix(nrow = nrow(data), ncol = 
    length(list(...)))) # set up dataframe for clusters
    for(i in list(...)) {
        kmeans <- kmeans(data[,i],centers = k)
        clusters[,i] <- kmeans$cluster
    }
    colnames(clusters) <- list(...)
    clusters
}

My question is: this seems to work when I only ask it to use consecutive columns, but not when I ask it to skip around some. 我的问题是:当我只要求它使用连续的列时,这似乎可行,但是当我要求它跳过某些列时,这似乎不起作用。 For instance, the first of the following works, but the second does not. 例如,以下第一个有效,但第二个无效。 Any idea how I can fix this? 知道我该如何解决吗?

# works how I want 
head(my.kmeans(data = mydata, k = 8, c(1,2), c(2,3), c(1,2,3)))

# doesn't work 
head(my.kmeans(data = mydata, k = 8, c(1,2), c(2,3), c(1,2,5)))

Also, I know people recommend using apply functions and staying away from for loops, but I don't know how to do this with an apply function. 另外,我知道人们建议使用Apply函数,并远离for循环,但是我不知道如何使用Apply函数来做到这一点。 Any advice on that would be much appreciated as well. 对此的任何建议也将不胜感激。

Thanks so much! 非常感谢!

Danny 丹尼

Building on @SatZ's comments, 以@SatZ的评论为基础,

set.seed(123)
mydata <- data.frame(a = rnorm(100,0,1), b = rnorm(100,0,1), c = 
                   rnorm(100,0,1), d = rnorm(100,0,1), e = 
                   rnorm(100,0,1)) 
mylist <- list(c(1,2), c(2,3), c(1,2,5))

set.seed(123)
my.kmeans <- function(data,k,list) {
  clusters <- data.frame(matrix(nrow = nrow(data), ncol = 
                              length(list))) # set up dataframe for 
                              clusters
  for(i in 1:length(list)) {
      kmeans <- kmeans(data[,list[[i]]],centers = k)
      clusters[,i] <- kmeans$cluster
  }
  colnames(clusters) <- list
  clusters
}

head(my.kmeans(data = mydata, k = 8, list = mylist))

暂无
暂无

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

相关问题 Annotation_logticks() 错误。 `$&lt;-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 8L, 7L, 6L, : 替换有 41 行,数据有 1 - Annotation_logticks() Error. Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 8L, 7L, 6L, : replacement has 41 rows, data has 1 `$<-.data.frame`(`*tmp*`, prediction, value = c(`1` = 1L, `2` = 1L, : replacement has 3462 rows, data has 1538 - Error in `$<-.data.frame`(`*tmp*`, prediction, value = c(`1` = 1L, `2` = 1L, : replacement has 3462 rows, data has 1538 `$<-.data.frame`(`*tmp*`, Predict, value = c(`1` = 1L, `2` = 1L, : replacement has 3500 rows, data has 1500 - Error in `$<-.data.frame`(`*tmp*`, Predict, value = c(`1` = 1L, `2` = 1L, : replacement has 3500 rows, data has 1500 Math.factor(c(1L,3L,4L,:&#39;round&#39;对因子无意义 - Error in Math.factor(c(1L, 3L, 4L, : ‘round’ not meaningful for factors KERAS IN R: Error in Summary.factor (c(1L, 1L, 1L, 1L,1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, : &#39;min&#39; 对因子没有意义 - KERAS IN R: Error in Summary.factor (c(1L, 1L, 1L, 1L,1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, : 'min' not meaningful for factors R 包 smcure 错误 `[[&lt;-.data.frame`(`*tmp*`, i, value = c(19L, 19L, 19L, 18L, 19L, : 替换有 2250 行,数据有 750 - R package smcure error Error in `[[<-.data.frame`(`*tmp*`, i, value = c(19L, 19L, 19L, 18L, 19L, : replacement has 2250 rows, data has 750 $$-。data.frame`(`* tmp *`,“ mode”,value = list(2L,3L))错误:替换有2行,数据有1行 - Error in `$<-.data.frame`(`*tmp*`, “mode”, value = list(2L, 3L)) : replacement has 2 rows, data has 1 使用upsetR时,如何解决“ $ &lt;-。data.frame`(`* tmp *`,“ x”,value = 1L)中的错误:替换有1行,数据有0”? - How to fix “Error in `$<-.data.frame`(`*tmp*`, ”x“, value = 1L) : replacement has 1 row, data has 0” when using upsetR? pmin和pmax中的错误“ [..data.frame`(每个,nas [,1L])中的错误:未定义的列已选择” - Error in pmin and pmax “Error in `[.data.frame`(each, nas[, 1L]) : undefined columns selected” ggadjustedcurves survminer 错误 if (xi &gt; xj) 1L else -1L - ggadjustedcurves survminer Error in if (xi > xj) 1L else -1L
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM