I'm doing a report for a data analysis and statistical learning subject.
The problem is in the clustering phase. I have to find the optimal number of clusters, and i already done. But when i try to display the graph with the following function it gives me an error:
nb<-NbClust(df, diss=NULL,distance="euclidean",method="average")
fviz_nbclust(nb)
Error in if (class(best_nc) == "numeric") print(best_nc) else if (class(best_nc) == :
the condition has length > 1
df is the standardized data set with only numerical data.
The error is referred to the fviz_nbclust
function.
I'd like to know what to do. I have already try to reinstall the packages, RStudio, everything but nothing. I also did, inside the NbClust()
function the min_nc
or max_nc
and stuff like that.
Thanks for your help.
> dput(df)
structure(c(-1.04227826822844, -0.171114188130418, -0.804688064565345,
1.37322213567972, 1.09603356473944, -0.042419494479573, 0.85844336107634,
-0.844286431842528, 1.53161560478845, 1.70980825753577, -0.844286431842528,
0.155572341906342, -0.636395003637318, -1.04227826822844, 0.363463770111553,
0.353564178292257, 1.86820172664451, 1.55141478842704, -0.715591738191684,
-0.824487248203937, 0.60105397377465, -0.299808881781262, 0.660451524690425,
1.37322213567972, -0.745290513649571, 1.23462785020958, -0.804688064565345,
-0.181013779949713, -0.685892962733796, 0.373363361930848, -0.487901126347882,
0.403062137388736, 1.32372417658324, -0.883884799119711, -0.903683982758303,
0.234769076460708, -0.270110106323375, 0.521857239220284, -1.04227826822844,
-0.299808881781262, -0.963081533674077, 0.422861321027327, 1.22472825839028,
-0.131515820853235, 0.848543769257044, 2.52157478671803, -1.04227826822844,
-0.735390921830275, 1.16533070747451, -0.903683982758303, -0.53739908544436,
0.85844336107634, 0.551556014678172, -0.992780309131965, -0.53739908544436,
-0.14141541267253, -0.873985207300416, 1.16533070747451, -0.854186023661824,
-0.289909289961967, 1.55141478842704, 0.205070301002821, 2.38298050124788,
-0.972981125493373, -0.53739908544436, 2.37308090942859, -0.745290513649571,
0.264467851918595, 0.917840911992114, -0.527499493625065, -0.963081533674077,
-0.774989289107458, -0.131515820853235, -1.04227826822844, -0.369106024516333,
1.26432662566747, 1.25442703384817, -0.963081533674077, -1.00267990095126,
-1.04227826822844, -0.963081533674077, 2.04639437939183, -0.428503575432107,
-0.210712555407601, -0.240411330865488, -0.289909289961967, -0.992780309131965,
0.175371525544933, -0.478001534528586, -0.844286431842528, -1.04227826822844,
-0.745290513649571, -0.438403167251403, 1.72960744117437, -0.854186023661824,
-0.240411330865488, -0.86408561548112, -1.04227826822844, 2.3433821339707,
1.28412580930606, -0.79478847274605, -0.972981125493373, -0.923483166396894,
-1.04227826822844, -1.00267990095126, 0.422861321027327, -1.04227826822844,
-1.04227826822844, -0.082017861756756, 1.30392499294465, -0.438403167251403,
-1.04227826822844, -0.289909289961967, -0.745290513649571, -0.93338275821619,
-0.586897044540839, -1.00267990095126, 2.67006866400746, -0.567097860902248,
-1.00267990095126, 1.43261968659549, 0.957439279269297, -0.280009698142671,
-1.02247908458985, -0.636395003637318, 0.808945401979861, 0.620853157413242,
-0.834386840023233, -1.03237867640915, 1.97709723665676, 1.76920580845155,
-0.616595819998726, 1.05643519746225, 0.561455606497468, -0.349306840877741,
2.3433821339707, 0.868342952895636, -1.04227826822844, 0.333764994653665,
0.0268776482554972, 1.8880009102831, 1.39302131931831, -0.626495411818022,
0.868342952895636, 0.640652341051833, 0.13577315826775, -0.0127207190216858,
-1.04227826822844, -0.497800718167177, -1.04227826822844, -0.963081533674077,
1.74940662481296, 0.502058055581693, -0.804688064565345, -0.458202350889994,
0.888142136534227, 1.62071193116211, -0.497800718167177, -1.04227826822844,
1.1752302992938, 1.75930621663225, -0.893784390939007, -0.972981125493373,
0.214969892822116, -0.161214596311122, 0.452560096485214, 0.779246626521974,
-1.00267990095126, -1.03237867640915, -0.0721182699374602, -0.00282112720239002,
-1.04227826822844, -0.695792554553092, -0.695792554553092, 0.898041728353523,
-0.547298677263656, -0.547298677263656, -0.86408561548112, -0.992780309131965,
-0.60669622817943, 0.987138054727185, -0.893784390939007, 1.11583274837803,
-0.695792554553092, 1.4128205029569, 0.0862751991712717, -0.804688064565345,
-0.844286431842528, 2.24438621577774, 0.0466768318940887, -0.992780309131965,
-0.735390921830275, -0.418603983612811, -0.909759252647386, 0.576794304173131,
-0.89841151557242, 0.644880726622926, -0.274285976449302, 0.531403355873268,
-0.637413562848207, 1.11013794669652, -0.104069920324816, -0.0700267090999183,
-0.399111084273926, 1.07609473547163, -0.206199553999508, -0.909759252647386,
1.04205152424673, 3.31159893923988, 0.0321029245747737, 0.372535036823747,
-0.875716041422489, -0.89841151557242, -0.455849769648755, 1.04205152424673,
-0.523936192098549, 0.724314886147686, -0.89841151557242, 1.93852275316902,
-0.841672830197591, -0.909759252647386, -0.909759252647386, -0.285633713524268,
-0.183504079849576, -0.909759252647386, 0.463316933423473, -0.89841151557242,
-0.909759252647386, 0.486012407573405, 1.25765852867108, -0.0586789720249525,
-0.887063778497454, -0.909759252647386, 1.96121822731896, 0.066146135799671,
0.066146135799671, 0.63353298954796, 0.826444519822378, 1.00800831302183,
-0.909759252647386, -0.887063778497454, -0.00194028665012365,
-0.421806558423858, 2.32434581371786, 0.747010360297618, -0.0813744461748841,
-0.875716041422489, -0.138113131549713, -0.89841151557242, -0.89841151557242,
1.28035400282101, -0.887063778497454, -0.523936192098549, 0.588142041248097,
0.792401308597481, 0.190971243624295, -0.89841151557242, 0.213666717774226,
0.406578248048644, -0.887063778497454, 0.349839562673815, 4.04920184911266,
-0.138113131549713, -0.887063778497454, -0.569327140398413, 2.50590960691731,
2.77825529671649, 0.190971243624295, 1.51865648139529, -0.228895028149439,
0.372535036823747, -0.909759252647386, -0.909759252647386, -0.887063778497454,
0.41792598512361, -0.138113131549713, -0.444502032573789, 0.179623506549329,
1.37113589942074, -0.68280451114807, 1.87043633071923, -0.671456774073105,
-0.535283929173515, -0.909759252647386, 0.179623506549329, -0.887063778497454,
1.53000421847026, -0.296981450599234, -0.592022614548344, 0.803749045672447,
-0.909759252647386, 1.8477408565693, 0.588142041248097, -0.750890933597865,
-0.796281881897728, -0.875716041422489, -0.909759252647386, -0.909759252647386,
0.213666717774226, -0.909759252647386, -0.89841151557242, -0.569327140398413,
-0.149460868624679, -0.353720135974063, -0.909759252647386, 1.22361531744618,
0.372535036823747, -0.853020567272557, -0.716847722372968, -0.909759252647386,
-0.887063778497454, -0.909759252647386, -0.853020567272557, 0.0774938728746368,
-0.0246357608000552, 0.41792598512361, -0.89841151557242, -0.864368304347523,
1.3484404252708, -0.115417657399781, -0.739543196522899, -0.89841151557242,
-0.206199553999508, 0.259057666074089, -0.478545243798686, 0.406578248048644,
0.894530942272173, 1.18957210622128, 1.51865648139529, -0.160808605699645,
-0.444502032573789, -0.739543196522899, 1.64348158921991, 0.463316933423473,
2.77825529671649, -0.89841151557242, 1.40517911064563, 2.65343018889187,
1.58674290384509, -0.716847722372968, -0.909759252647386, -0.489892980873652,
-0.864368304347523, -0.909759252647386, 0.565446567098165, -0.637413562848207,
-0.887063778497454, -0.784934144822762, 2.40377997324262, -0.342372398899097,
-0.796281881897728, -0.909759252647386, -0.0586789720249525,
0.860487731047276, 0.259057666074089, -0.773586407747797, 1.09879020962156,
-0.89841151557242, -0.240242765224405, 0.213666717774226, -0.523936192098549,
-0.750890933597865, 2.00660917561882, -0.614718088698276, -0.909759252647386,
-0.89841151557242, -0.68280451114807, 0.84913999397231, -0.887063778497454,
-0.671456774073105, -0.115417657399781, -0.455849769648755, -0.81897735604766,
1.76830669704454, 0.610837515398028, 0.508707881723336, -0.853020567272557,
0.871835468122241, -0.523936192098549, 0.225014454849192, -0.716847722372968,
0.213666717774226, -0.89841151557242, -0.909759252647386, -0.705499985298002,
-0.716847722372968, -0.61320221823091, 0.0533700974209474, -0.44970334835404,
3.29819382266961, -0.0598214278784247, -0.0598214278784247, 2.15370173353152,
-0.487433856787164, 2.04051020823215, 1.77639664920028, -0.562894873653412,
0.0156395889878234, -0.537741201364662, -0.61320221823091, -0.173012953177797,
-0.0975519363115487, 2.04051020823215, -0.525164365220288, -0.462280184498414,
-0.562894873653412, -0.525164365220288, -0.525164365220288, -0.185589789322171,
-0.42454967606529, -0.61320221823091, 0.556443543195934, -0.537741201364662,
-0.61320221823091, -0.537741201364662, -0.42454967606529, -0.61320221823091,
-0.575471709797786, 0.631904560062183, -0.61320221823091, -0.61320221823091,
1.53743676245716, -0.525164365220288, -0.588048545942161, -0.61320221823091,
-0.512587529075913, 0.304906820308441, -0.487433856787164, 2.56873732629588,
-0.562894873653412, 0.795403429939053, 1.05951698897092, -0.61320221823091,
-0.61320221823091, 2.87058139376088, -0.588048545942161, -0.298781314621544,
-0.512587529075913, -0.588048545942161, -0.61320221823091, -0.600625382086536,
2.30462376726401, -0.588048545942161, 0.116254278142821, -0.600625382086536,
-0.61320221823091, 0.594174051629059, 4.02765031904335, 0.116254278142821,
-0.61320221823091, 1.24816953113654, 1.57516727089028, -0.500010692931538,
2.11597122509839, -0.273627642332794, -0.600625382086536, -0.600625382086536,
-0.361665495343417, -0.61320221823091, -0.61320221823091, -0.600625382086536,
1.70093563233403, 0.35521416488594, -0.61320221823091, -0.600625382086536,
-0.61320221823091, -0.600625382086536, 1.44939890944654, -0.512587529075913,
2.35493111184151, -0.512587529075913, -0.42454967606529, -0.61320221823091,
-0.474857020642789, -0.600625382086536, -0.61320221823091, -0.61320221823091,
-0.550318037509037, 0.9211717913828, 0.153984786575945, -0.23589713389967,
-0.61320221823091, -0.600625382086536, -0.61320221823091, 0.0785237697096968,
2.78254354075025, -0.575471709797786, -0.61320221823091, -0.575471709797786,
-0.61320221823091, -0.61320221823091, 0.883441282949676, -0.61320221823091,
-0.600625382086536, -0.399396003776541, -0.562894873653412, -0.399396003776541,
-0.61320221823091, -0.525164365220288, 0.984055972104674, -0.500010692931538,
-0.562894873653412, -0.575471709797786, -0.61320221823091, -0.525164365220288,
-0.588048545942161, 1.7638198130559, 1.57516727089028, -0.61320221823091,
-0.61320221823091, -0.600625382086536, -0.537741201364662, 0.996632808249048,
-0.61320221823091, -0.600625382086536, -0.336511823054668, -0.399396003776541,
-0.61320221823091, 0.304906820308441, -0.361665495343417, -0.61320221823091,
0.0785237697096968, 3.63776839856773, -0.537741201364662, -0.512587529075913,
-0.399396003776541, 1.47455258173529, 0.292329984164066, -0.600625382086536,
-0.223320297755295, 0.267176311875317, -0.487433856787164, -0.323934986910293,
-0.61320221823091, 1.13497800583717, -0.61320221823091, -0.537741201364662,
0.971479135960299, 0.0156395889878234, -0.600625382086536, -0.487433856787164,
0.833133938372178, 2.84542772147213, -0.61320221823091, -0.61320221823091,
0.392944673319064, 0.782826593794679, -0.61320221823091, -0.588048545942161,
-0.537741201364662, -0.600625382086536, 1.7135124684784, 2.89573506604962,
-0.42454967606529, -0.575471709797786, -0.61320221823091, 0.455828854040937,
-0.575471709797786, -0.386819167632166, -0.562894873653412, -0.537741201364662,
-0.374242331487792, -0.537741201364662, -0.223320297755295, -0.512587529075913,
-0.575471709797786, -0.0598214278784247, -0.562894873653412,
1.82670399377778, -0.61320221823091, 0.430675181752188, 2.14112489738714,
-0.525164365220288, -0.487433856787164, -0.588048545942161, -0.61320221823091,
-0.61320221823091, -0.575471709797786, -0.575471709797786, -1.04004516290537,
0.0154246956882285, -1.12123515202795, 2.04517442375284, 0.28605799276351,
0.0154246956882285, 0.935577905744186, -0.282271931094581, 1.50390782960228,
1.31446452164958, -0.958855173782785, 0.394311311593623, -0.769411865830088,
-1.04004516290537, 0.394311311593623, 2.5864410179034, 1.53097115930981,
0.529627960131263, -1.01298183319784, -1.20242514115054, -0.282271931094581,
-0.0657652934343561, 0.150741344225869, 0.637881278961376, -1.14829848173548,
1.47684449989475, -0.146955282556941, 0.394311311593623, -0.228145271679525,
-0.228145271679525, -0.715285206415032, 0.258994663055982, 0.908514576036657,
-0.823538525245144, -1.20242514115054, 0.746134597791489, 0.0424880253957566,
-0.174018612264469, -1.28361513027312, -0.850601854952673, 0.28605799276351,
-0.119891952849412, 1.44978117018722, -0.174018612264469, 0.908514576036657,
1.88279444550767, -1.04004516290537, -0.688221876707504, 1.50390782960228,
-1.01298183319784, 0.475501300716207, 0.367247981886095, -0.174018612264469,
-1.25655180056559, -0.715285206415032, 0.258994663055982, -1.17536181144301,
1.26033786223452, -1.12123515202795, -0.769411865830088, 1.39565451077216,
1.88279444550767, 1.09795788398935, -0.661158546999976, 0.150741344225869,
1.74747779697003, -0.823538525245144, 0.935577905744186, 1.9098577752152,
-0.715285206415032, -1.25655180056559, -0.634095217292447, 0.610817949253848,
0.28605799276351, -0.498778568754807, 1.74747779697003, 0.475501300716207,
-0.715285206415032, -1.28361513027312, -0.769411865830088, -1.25655180056559,
1.77454112667756, -0.634095217292447, 0.448437971008679, -0.390525249924694,
0.58375461954632, -1.17536181144301, 0.529627960131263, -0.823538525245144,
-1.04004516290537, -1.04004516290537, -0.661158546999976, 0.367247981886095,
1.53097115930981, -0.796475195537616, -0.552905228169863, -0.471715239047278,
-1.04004516290537, 2.18049107229048, 1.77454112667756, -1.09417182232043,
-0.904728514367729, -1.22948847085807, -1.04004516290537, -1.14829848173548,
0.475501300716207, 1.39565451077216, -0.769411865830088, -0.607031887584919,
0.177804673933397, -0.688221876707504, -1.04004516290537, 0.0154246956882285,
0.0154246956882285, -1.17536181144301, -0.958855173782785, -1.28361513027312,
0.529627960131263, -1.04004516290537, -1.25655180056559, 1.233274532527,
1.20621120281947, -0.363461920217166, -1.28361513027312, 1.15208454340441,
0.58375461954632, 0.502564630423735, -1.12123515202795, -1.22948847085807,
0.556691289838792, 0.637881278961376, -0.904728514367729, 0.664944608668904,
0.340184652178566, -0.0657652934343561, 1.63922447813992, 1.66628780784745,
-1.0671084926129, 1.34152785135711, 0.394311311593623, 1.50390782960228,
1.80160445638509, 0.529627960131263, 0.773197927499017, 1.42271784047969,
0.394311311593623, -0.607031887584919, -0.769411865830088, -0.174018612264469,
-1.28361513027312, -1.22948847085807, 1.28740119194205, -0.201081941971997,
0.502564630423735, -0.904728514367729, 1.77454112667756, 1.55803448901733,
-0.985918503490313, -1.04004516290537, 0.908514576036657, 1.39565451077216,
-0.715285206415032, -0.850601854952673, 0.204868003640925, -0.0387019637268278,
0.637881278961376, 1.44978117018722, -1.04004516290537, -1.22948847085807,
0.421374641301151, -0.255208601387053, -1.28361513027312, -0.958855173782785,
-1.01298183319784, 0.421374641301151, -0.958855173782785, -0.931791844075257,
-0.715285206415032, -1.04004516290537, 0.935577905744186, 1.09795788398935,
-0.552905228169863, 1.50390782960228, 0.231931333348454, 1.0438312245743,
0.475501300716207, -0.661158546999976, -1.0671084926129, 0.773197927499017,
-0.769411865830088, -1.28361513027312, -0.634095217292447, -0.0387019637268278
), .Dim = c(193L, 4L), .Dimnames = list(NULL, c("beer_servings",
"spirit_servings", "wine_servings", "total_litres_of_pure_alcohol"
)), "`scaled:center`" = c(beer_servings = 106.284974093264, spirit_servings = 81.1709844559585,
wine_servings = 49.7564766839378, total_litres_of_pure_alcohol = 4.84300518134715
), "`scaled:scale`" = c(beer_servings = 101.014265866079, spirit_servings = 88.1232966003502,
wine_servings = 79.511252951107, total_litres_of_pure_alcohol = 3.69503682956585
))
I had the same problem and it turned out that the function
fviz_nbclust
(and also the hidden function.viz_NbClust
as a matter of fact) is written wrongly. Specifically, the problem is the testsif (class(best_nc) == "numeric")
andif (class(best_nc) == "matrix")
in the both functions which are nonsense as the objectbest_nc
, which is the element of the NbClust resultnb$Best.nc
, has actually two classes:So, the tests will fail as
if
can take only one logical value as the condition, but the tests produce two values:My solution: I had to fix the functions
fviz_nbclust
and.viz_NbClust
so that the tests look likeif (any(class(best_nc) == "numeric") )
andif (any(class(best_nc) == "matrix") )
. Here is a working code:Result: