3 Replies Latest reply on Sep 27, 2016 3:12 AM by Hardi Saputra

    Return specific cluster belong to new data set (k-means using R)

    Hardi Saputra

      Hi all,
      I am performing k-means in tableau desktop. I could successfully return the new field consist of number of cluster for each case.
      Now I am modifying the syntax to return an integer (cluster number) which my new input belong to. I just found the way through someone's post but it didnt work.  here is my code:

       

      SCRIPT_REAL(

      'set.seed(.arg6[1]);

      num.k= .arg5[1];

      data=data.frame(.arg1,.arg2,.arg3,.arg4);

      cl = kmeans(data,num.k);

       

      x = c(.arg7,.arg8,.arg9,.arg10);

      closest.cluster = function(x) {

        cluster.dist <- apply(cl$centers, 1, function(y) sqrt(sum((x-y)^2)))

      return(which.min(cluster.dist)[1])

        }',

      SUM([Holiday Type Value]),SUM([NumberOfPersons]),SUM([Season Value]),SUM([Price]),[Number of K],[Seed],[Filter Holiday Type],[Filter Number of Persons],[Filter Season],[Filter Price])

       

      where did i do wrong? thanks in advance