Cbind result of aplucter to dataframe in R












1















I perform cluster analysis and i must use apcluster library



library(apcluster)

## create four synthetic 2D clusters
cl1 <- cbind(rnorm(30, 0.3, 0.05), rnorm(30, 0.7, 0.04))
cl2 <- cbind(rnorm(30, 0.7, 0.04), rnorm(30, 0.4, .05))
cl3 <- cbind(rnorm(20, 0.50, 0.03), rnorm(20, 0.72, 0.03))
cl4 <- cbind(rnorm(25, 0.50, 0.03), rnorm(25, 0.42, 0.04))
x <- rbind(cl1, cl2, cl3, cl4)

## run apcluster() (you may replace the Euclidean distance by a different
## distance, e.g. driving distance, driving time)
apres <- apcluster(negDistMat(r=2), x, q=0)


After the clustering, selected cluster i want to join to x data.frame to see
observation and what it cluster.
I did so



cbind(x, mycluster=apres$Clusters)


and error



Error in apres$Clusters : $ operator not defined for this S4 class


How should i do?



 [,1]           [,2]      cluster
[1,] 0.3671142 0.6204475 2









share|improve this question



























    1















    I perform cluster analysis and i must use apcluster library



    library(apcluster)

    ## create four synthetic 2D clusters
    cl1 <- cbind(rnorm(30, 0.3, 0.05), rnorm(30, 0.7, 0.04))
    cl2 <- cbind(rnorm(30, 0.7, 0.04), rnorm(30, 0.4, .05))
    cl3 <- cbind(rnorm(20, 0.50, 0.03), rnorm(20, 0.72, 0.03))
    cl4 <- cbind(rnorm(25, 0.50, 0.03), rnorm(25, 0.42, 0.04))
    x <- rbind(cl1, cl2, cl3, cl4)

    ## run apcluster() (you may replace the Euclidean distance by a different
    ## distance, e.g. driving distance, driving time)
    apres <- apcluster(negDistMat(r=2), x, q=0)


    After the clustering, selected cluster i want to join to x data.frame to see
    observation and what it cluster.
    I did so



    cbind(x, mycluster=apres$Clusters)


    and error



    Error in apres$Clusters : $ operator not defined for this S4 class


    How should i do?



     [,1]           [,2]      cluster
    [1,] 0.3671142 0.6204475 2









    share|improve this question

























      1












      1








      1








      I perform cluster analysis and i must use apcluster library



      library(apcluster)

      ## create four synthetic 2D clusters
      cl1 <- cbind(rnorm(30, 0.3, 0.05), rnorm(30, 0.7, 0.04))
      cl2 <- cbind(rnorm(30, 0.7, 0.04), rnorm(30, 0.4, .05))
      cl3 <- cbind(rnorm(20, 0.50, 0.03), rnorm(20, 0.72, 0.03))
      cl4 <- cbind(rnorm(25, 0.50, 0.03), rnorm(25, 0.42, 0.04))
      x <- rbind(cl1, cl2, cl3, cl4)

      ## run apcluster() (you may replace the Euclidean distance by a different
      ## distance, e.g. driving distance, driving time)
      apres <- apcluster(negDistMat(r=2), x, q=0)


      After the clustering, selected cluster i want to join to x data.frame to see
      observation and what it cluster.
      I did so



      cbind(x, mycluster=apres$Clusters)


      and error



      Error in apres$Clusters : $ operator not defined for this S4 class


      How should i do?



       [,1]           [,2]      cluster
      [1,] 0.3671142 0.6204475 2









      share|improve this question














      I perform cluster analysis and i must use apcluster library



      library(apcluster)

      ## create four synthetic 2D clusters
      cl1 <- cbind(rnorm(30, 0.3, 0.05), rnorm(30, 0.7, 0.04))
      cl2 <- cbind(rnorm(30, 0.7, 0.04), rnorm(30, 0.4, .05))
      cl3 <- cbind(rnorm(20, 0.50, 0.03), rnorm(20, 0.72, 0.03))
      cl4 <- cbind(rnorm(25, 0.50, 0.03), rnorm(25, 0.42, 0.04))
      x <- rbind(cl1, cl2, cl3, cl4)

      ## run apcluster() (you may replace the Euclidean distance by a different
      ## distance, e.g. driving distance, driving time)
      apres <- apcluster(negDistMat(r=2), x, q=0)


      After the clustering, selected cluster i want to join to x data.frame to see
      observation and what it cluster.
      I did so



      cbind(x, mycluster=apres$Clusters)


      and error



      Error in apres$Clusters : $ operator not defined for this S4 class


      How should i do?



       [,1]           [,2]      cluster
      [1,] 0.3671142 0.6204475 2






      r dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 10:03









      d-maxd-max

      728




      728
























          1 Answer
          1






          active

          oldest

          votes


















          3














          The first issue is that apres isn't an S3 class object and using $ isn't going to work. See apres and you will see @ used in a similar fashion. In particular, as to get the resulting clusters, you want



          cls <- apres@clusters


          Next thing is that cls is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be



          clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
          head(clsNm)
          # 1 1 1 1 1 1
          # 1 2 3 4 5 6


          Then we may do what you tried



          head(cbind(x, cluster = as.integer(names(clsNm))), 2)
          # cluster
          # [1,] 0.2526693 0.6764543 1
          # [2,] 0.2781545 0.6774726 1





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350167%2fcbind-result-of-aplucter-to-dataframe-in-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            The first issue is that apres isn't an S3 class object and using $ isn't going to work. See apres and you will see @ used in a similar fashion. In particular, as to get the resulting clusters, you want



            cls <- apres@clusters


            Next thing is that cls is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be



            clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
            head(clsNm)
            # 1 1 1 1 1 1
            # 1 2 3 4 5 6


            Then we may do what you tried



            head(cbind(x, cluster = as.integer(names(clsNm))), 2)
            # cluster
            # [1,] 0.2526693 0.6764543 1
            # [2,] 0.2781545 0.6774726 1





            share|improve this answer




























              3














              The first issue is that apres isn't an S3 class object and using $ isn't going to work. See apres and you will see @ used in a similar fashion. In particular, as to get the resulting clusters, you want



              cls <- apres@clusters


              Next thing is that cls is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be



              clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
              head(clsNm)
              # 1 1 1 1 1 1
              # 1 2 3 4 5 6


              Then we may do what you tried



              head(cbind(x, cluster = as.integer(names(clsNm))), 2)
              # cluster
              # [1,] 0.2526693 0.6764543 1
              # [2,] 0.2781545 0.6774726 1





              share|improve this answer


























                3












                3








                3







                The first issue is that apres isn't an S3 class object and using $ isn't going to work. See apres and you will see @ used in a similar fashion. In particular, as to get the resulting clusters, you want



                cls <- apres@clusters


                Next thing is that cls is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be



                clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
                head(clsNm)
                # 1 1 1 1 1 1
                # 1 2 3 4 5 6


                Then we may do what you tried



                head(cbind(x, cluster = as.integer(names(clsNm))), 2)
                # cluster
                # [1,] 0.2526693 0.6764543 1
                # [2,] 0.2781545 0.6774726 1





                share|improve this answer













                The first issue is that apres isn't an S3 class object and using $ isn't going to work. See apres and you will see @ used in a similar fashion. In particular, as to get the resulting clusters, you want



                cls <- apres@clusters


                Next thing is that cls is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be



                clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
                head(clsNm)
                # 1 1 1 1 1 1
                # 1 2 3 4 5 6


                Then we may do what you tried



                head(cbind(x, cluster = as.integer(names(clsNm))), 2)
                # cluster
                # [1,] 0.2526693 0.6764543 1
                # [2,] 0.2781545 0.6774726 1






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 17 '18 at 10:14









                Julius VainoraJulius Vainora

                34.7k76079




                34.7k76079






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350167%2fcbind-result-of-aplucter-to-dataframe-in-r%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)