I am trying to analyse the iris
data-frame with clustering analysis. I was provided here a way to use Map
in R to map the data onto sets of hyperparameter combinations listed by expand.grid
and collect all results in one table.
我现在想同时针对数据框的修改版本执行此操作。因此,例如:
acc <- function(x){
first = sum(x)
second = sum(x^2)
return(list(First=first,Second=second))
}
tests <- expand.grid(Clustering_Algorithm=c("ward.D","ward.D2","single","complete","average","mcquitty","median","centroid"),
DS=c("iris0","iris1","iris2"))
iris0 <- iris
iris1 <- cbind(log(iris[,1:4]),iris[5])
iris2 <- cbind(sqrt(iris[,1:4]),iris[5])
Table <- Map(function(x, ds){acc(table(ds$Species, cutree(hclust(dist(ds.[,1:4]),method=x),3)))},tests[[1]], tests[[2]])
This fails to run for me with the error "Error in ds$Species : $ operator is invalid for atomic vectors". I've tried writing as.character(tests[[2]])
instead which had the same error message. I've even tried options like ds %>% .[,"Species"])
and ds %>% .[,1:4]
, in which case I get a different error message: " Error in .[, "Species"] : incorrect number of dimensions".
知道如何解决吗?
编辑:
Just tried to use lapply
over the list DS
and after putting rest of the above in a function, and that gave me the exact same error message.
The issue is that 'ds' is a string, we need to get the value with
mget
orget
It can be made more compact if we use
mget