Round dbl to nearlest integer and convert to ordered factor in R











up vote
0
down vote

favorite












I have a numeric column within a dataframe that I want to round to the nearest integer and then convert into an ordered factor. This seems fairly straight forward, but I can't figure out why this isn't working as expected.



# example data
tmp <- structure(list(variable1 = c(X1 = 3.66666666666667, X2 = 2.66666666666667,
X3 = 3.33333333333333, X4 = 3, X5 = 2, X6 = 2, X7 = NA, X8 = 3.33333333333333,
X9 = 2.66666666666667, X10 = 4, X11 = 3.66666666666667, X12 = 3,
X13 = 3.66666666666667, X14 = 3.33333333333333, X15 = 3.33333333333333,
X16 = 4, X17 = 3.33333333333333, X18 = 3, X19 = 3, X20 = 3, X21 = 3,
X22 = 3.33333333333333, X23 = 3.33333333333333, X24 = 2.66666666666667,
X25 = 2, X26 = 3.33333333333333, X27 = 3, X28 = 3, X29 = 2.66666666666667)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -29L))

# round to the nearest integer
scores <- round(tmp, digits = 0)

# convert to an ordered factor
factors <-
ordered(
scores,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)


This code returns the following:



variable1 
<NA>
Levels: Strongly Disagree < Disagree < Agree < Strongly Agree


I thought it was because of the NA in my dataset, but I still get the same thing after omitting NA.



What am I doing wrong?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a numeric column within a dataframe that I want to round to the nearest integer and then convert into an ordered factor. This seems fairly straight forward, but I can't figure out why this isn't working as expected.



    # example data
    tmp <- structure(list(variable1 = c(X1 = 3.66666666666667, X2 = 2.66666666666667,
    X3 = 3.33333333333333, X4 = 3, X5 = 2, X6 = 2, X7 = NA, X8 = 3.33333333333333,
    X9 = 2.66666666666667, X10 = 4, X11 = 3.66666666666667, X12 = 3,
    X13 = 3.66666666666667, X14 = 3.33333333333333, X15 = 3.33333333333333,
    X16 = 4, X17 = 3.33333333333333, X18 = 3, X19 = 3, X20 = 3, X21 = 3,
    X22 = 3.33333333333333, X23 = 3.33333333333333, X24 = 2.66666666666667,
    X25 = 2, X26 = 3.33333333333333, X27 = 3, X28 = 3, X29 = 2.66666666666667)),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA, -29L))

    # round to the nearest integer
    scores <- round(tmp, digits = 0)

    # convert to an ordered factor
    factors <-
    ordered(
    scores,
    levels = c(1, 2, 3, 4),
    labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
    )


    This code returns the following:



    variable1 
    <NA>
    Levels: Strongly Disagree < Disagree < Agree < Strongly Agree


    I thought it was because of the NA in my dataset, but I still get the same thing after omitting NA.



    What am I doing wrong?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a numeric column within a dataframe that I want to round to the nearest integer and then convert into an ordered factor. This seems fairly straight forward, but I can't figure out why this isn't working as expected.



      # example data
      tmp <- structure(list(variable1 = c(X1 = 3.66666666666667, X2 = 2.66666666666667,
      X3 = 3.33333333333333, X4 = 3, X5 = 2, X6 = 2, X7 = NA, X8 = 3.33333333333333,
      X9 = 2.66666666666667, X10 = 4, X11 = 3.66666666666667, X12 = 3,
      X13 = 3.66666666666667, X14 = 3.33333333333333, X15 = 3.33333333333333,
      X16 = 4, X17 = 3.33333333333333, X18 = 3, X19 = 3, X20 = 3, X21 = 3,
      X22 = 3.33333333333333, X23 = 3.33333333333333, X24 = 2.66666666666667,
      X25 = 2, X26 = 3.33333333333333, X27 = 3, X28 = 3, X29 = 2.66666666666667)),
      class = c("tbl_df", "tbl", "data.frame"),
      row.names = c(NA, -29L))

      # round to the nearest integer
      scores <- round(tmp, digits = 0)

      # convert to an ordered factor
      factors <-
      ordered(
      scores,
      levels = c(1, 2, 3, 4),
      labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
      )


      This code returns the following:



      variable1 
      <NA>
      Levels: Strongly Disagree < Disagree < Agree < Strongly Agree


      I thought it was because of the NA in my dataset, but I still get the same thing after omitting NA.



      What am I doing wrong?










      share|improve this question













      I have a numeric column within a dataframe that I want to round to the nearest integer and then convert into an ordered factor. This seems fairly straight forward, but I can't figure out why this isn't working as expected.



      # example data
      tmp <- structure(list(variable1 = c(X1 = 3.66666666666667, X2 = 2.66666666666667,
      X3 = 3.33333333333333, X4 = 3, X5 = 2, X6 = 2, X7 = NA, X8 = 3.33333333333333,
      X9 = 2.66666666666667, X10 = 4, X11 = 3.66666666666667, X12 = 3,
      X13 = 3.66666666666667, X14 = 3.33333333333333, X15 = 3.33333333333333,
      X16 = 4, X17 = 3.33333333333333, X18 = 3, X19 = 3, X20 = 3, X21 = 3,
      X22 = 3.33333333333333, X23 = 3.33333333333333, X24 = 2.66666666666667,
      X25 = 2, X26 = 3.33333333333333, X27 = 3, X28 = 3, X29 = 2.66666666666667)),
      class = c("tbl_df", "tbl", "data.frame"),
      row.names = c(NA, -29L))

      # round to the nearest integer
      scores <- round(tmp, digits = 0)

      # convert to an ordered factor
      factors <-
      ordered(
      scores,
      levels = c(1, 2, 3, 4),
      labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
      )


      This code returns the following:



      variable1 
      <NA>
      Levels: Strongly Disagree < Disagree < Agree < Strongly Agree


      I thought it was because of the NA in my dataset, but I still get the same thing after omitting NA.



      What am I doing wrong?







      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 3:53









      CurtLH

      6921032




      6921032
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          It is because you are passing a data frame and not a vector to ordered():



          str(scores)
          # Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
          # $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...


          But ordered() needs a vector:




          ordered(x, ...)



          x: a vector of data, usually taking a small number of distinct
          values.




          Use scores$variable1 instead:



          factors <-
          ordered(
          scores$variable1,
          levels = c(1, 2, 3, 4),
          labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
          )


          factors
          # [1] Strongly Agree Agree Agree Agree Disagree
          # [6] Disagree <NA> Agree Agree Strongly Agree
          # [11] Strongly Agree Agree Strongly Agree Agree Agree
          # [16] Strongly Agree Agree Agree Agree Agree
          # [21] Agree Agree Agree Agree Disagree
          # [26] Agree Agree Agree Agree
          # Levels: Strongly Disagree < Disagree < Agree < Strongly Agree





          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',
            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%2f53245699%2fround-dbl-to-nearlest-integer-and-convert-to-ordered-factor-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








            up vote
            1
            down vote



            accepted










            It is because you are passing a data frame and not a vector to ordered():



            str(scores)
            # Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
            # $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...


            But ordered() needs a vector:




            ordered(x, ...)



            x: a vector of data, usually taking a small number of distinct
            values.




            Use scores$variable1 instead:



            factors <-
            ordered(
            scores$variable1,
            levels = c(1, 2, 3, 4),
            labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
            )


            factors
            # [1] Strongly Agree Agree Agree Agree Disagree
            # [6] Disagree <NA> Agree Agree Strongly Agree
            # [11] Strongly Agree Agree Strongly Agree Agree Agree
            # [16] Strongly Agree Agree Agree Agree Agree
            # [21] Agree Agree Agree Agree Disagree
            # [26] Agree Agree Agree Agree
            # Levels: Strongly Disagree < Disagree < Agree < Strongly Agree





            share|improve this answer

























              up vote
              1
              down vote



              accepted










              It is because you are passing a data frame and not a vector to ordered():



              str(scores)
              # Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
              # $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...


              But ordered() needs a vector:




              ordered(x, ...)



              x: a vector of data, usually taking a small number of distinct
              values.




              Use scores$variable1 instead:



              factors <-
              ordered(
              scores$variable1,
              levels = c(1, 2, 3, 4),
              labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
              )


              factors
              # [1] Strongly Agree Agree Agree Agree Disagree
              # [6] Disagree <NA> Agree Agree Strongly Agree
              # [11] Strongly Agree Agree Strongly Agree Agree Agree
              # [16] Strongly Agree Agree Agree Agree Agree
              # [21] Agree Agree Agree Agree Disagree
              # [26] Agree Agree Agree Agree
              # Levels: Strongly Disagree < Disagree < Agree < Strongly Agree





              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                It is because you are passing a data frame and not a vector to ordered():



                str(scores)
                # Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
                # $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...


                But ordered() needs a vector:




                ordered(x, ...)



                x: a vector of data, usually taking a small number of distinct
                values.




                Use scores$variable1 instead:



                factors <-
                ordered(
                scores$variable1,
                levels = c(1, 2, 3, 4),
                labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
                )


                factors
                # [1] Strongly Agree Agree Agree Agree Disagree
                # [6] Disagree <NA> Agree Agree Strongly Agree
                # [11] Strongly Agree Agree Strongly Agree Agree Agree
                # [16] Strongly Agree Agree Agree Agree Agree
                # [21] Agree Agree Agree Agree Disagree
                # [26] Agree Agree Agree Agree
                # Levels: Strongly Disagree < Disagree < Agree < Strongly Agree





                share|improve this answer












                It is because you are passing a data frame and not a vector to ordered():



                str(scores)
                # Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
                # $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...


                But ordered() needs a vector:




                ordered(x, ...)



                x: a vector of data, usually taking a small number of distinct
                values.




                Use scores$variable1 instead:



                factors <-
                ordered(
                scores$variable1,
                levels = c(1, 2, 3, 4),
                labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
                )


                factors
                # [1] Strongly Agree Agree Agree Agree Disagree
                # [6] Disagree <NA> Agree Agree Strongly Agree
                # [11] Strongly Agree Agree Strongly Agree Agree Agree
                # [16] Strongly Agree Agree Agree Agree Agree
                # [21] Agree Agree Agree Agree Disagree
                # [26] Agree Agree Agree Agree
                # Levels: Strongly Disagree < Disagree < Agree < Strongly Agree






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 4:13









                prosoitos

                822219




                822219






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245699%2fround-dbl-to-nearlest-integer-and-convert-to-ordered-factor-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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python