Add column which contains binned values of an integer column












19















I have a dataframe with a few columns, one of those columns is ranks, an integer between 1 and 20. I want to create another column that contains a bin value like "1-4", "5-10", "11-15", "16-20".



What is the most effective way to do this?



the data frame that I have looks like this(.csv format):



rank,name,info
1,steve,red
3,joe,blue
6,john,green
3,liz,yellow
15,jon,pink


and I want to add another column to the dataframe, so it would be like this:



rank,name,info,binValue
1,steve,red,"1-4"
3,joe,blue,"1-4"
6,john,green, "5-10"
3,liz,yellow,"1-4"
15,jon,pink,"11-15"


The way I am doing it now is not working, as I would like to keep the data.frame intact, and just add another column if the value of df$ranked is within a given range. thank you.










share|improve this question




















  • 2





    Related: Create categorical variable in R based on range and in R, how to distribution data into different group

    – Joshua Ulrich
    Apr 6 '11 at 18:08
















19















I have a dataframe with a few columns, one of those columns is ranks, an integer between 1 and 20. I want to create another column that contains a bin value like "1-4", "5-10", "11-15", "16-20".



What is the most effective way to do this?



the data frame that I have looks like this(.csv format):



rank,name,info
1,steve,red
3,joe,blue
6,john,green
3,liz,yellow
15,jon,pink


and I want to add another column to the dataframe, so it would be like this:



rank,name,info,binValue
1,steve,red,"1-4"
3,joe,blue,"1-4"
6,john,green, "5-10"
3,liz,yellow,"1-4"
15,jon,pink,"11-15"


The way I am doing it now is not working, as I would like to keep the data.frame intact, and just add another column if the value of df$ranked is within a given range. thank you.










share|improve this question




















  • 2





    Related: Create categorical variable in R based on range and in R, how to distribution data into different group

    – Joshua Ulrich
    Apr 6 '11 at 18:08














19












19








19


5






I have a dataframe with a few columns, one of those columns is ranks, an integer between 1 and 20. I want to create another column that contains a bin value like "1-4", "5-10", "11-15", "16-20".



What is the most effective way to do this?



the data frame that I have looks like this(.csv format):



rank,name,info
1,steve,red
3,joe,blue
6,john,green
3,liz,yellow
15,jon,pink


and I want to add another column to the dataframe, so it would be like this:



rank,name,info,binValue
1,steve,red,"1-4"
3,joe,blue,"1-4"
6,john,green, "5-10"
3,liz,yellow,"1-4"
15,jon,pink,"11-15"


The way I am doing it now is not working, as I would like to keep the data.frame intact, and just add another column if the value of df$ranked is within a given range. thank you.










share|improve this question
















I have a dataframe with a few columns, one of those columns is ranks, an integer between 1 and 20. I want to create another column that contains a bin value like "1-4", "5-10", "11-15", "16-20".



What is the most effective way to do this?



the data frame that I have looks like this(.csv format):



rank,name,info
1,steve,red
3,joe,blue
6,john,green
3,liz,yellow
15,jon,pink


and I want to add another column to the dataframe, so it would be like this:



rank,name,info,binValue
1,steve,red,"1-4"
3,joe,blue,"1-4"
6,john,green, "5-10"
3,liz,yellow,"1-4"
15,jon,pink,"11-15"


The way I am doing it now is not working, as I would like to keep the data.frame intact, and just add another column if the value of df$ranked is within a given range. thank you.







r r-faq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 13:11









Henrik

42.1k994110




42.1k994110










asked Apr 6 '11 at 17:11









wespiserAwespiserA

2,15452034




2,15452034








  • 2





    Related: Create categorical variable in R based on range and in R, how to distribution data into different group

    – Joshua Ulrich
    Apr 6 '11 at 18:08














  • 2





    Related: Create categorical variable in R based on range and in R, how to distribution data into different group

    – Joshua Ulrich
    Apr 6 '11 at 18:08








2




2





Related: Create categorical variable in R based on range and in R, how to distribution data into different group

– Joshua Ulrich
Apr 6 '11 at 18:08





Related: Create categorical variable in R based on range and in R, how to distribution data into different group

– Joshua Ulrich
Apr 6 '11 at 18:08












3 Answers
3






active

oldest

votes


















42














See ?cut and specify breaks (and maybe labels).



x$bins <- cut(x$rank, breaks=c(0,4,10,15), labels=c("1-4","5-10","10-15"))
x
# rank name info bins
# 1 1 steve red 1-4
# 2 3 joe blue 1-4
# 3 6 john green 5-10
# 4 3 liz yellow 1-4
# 5 15 jon pink 10-15





share|improve this answer
























  • how can be do it using for loop and assign like function for multiple columns in one go?

    – I_m_LeMarque
    Nov 5 '18 at 15:37



















6














dat <- "rank,name,info
1,steve,red
3,joe,blue
6,john,green
3,liz,yellow
15,jon,pink"

x <- read.table(textConnection(dat), header=TRUE, sep=",", stringsAsFactors=FALSE)
x$bins <- cut(x$rank, breaks=seq(0, 20, 5), labels=c("1-5", "6-10", "11-15", "16-20"))
x

rank name info bins
1 1 steve red 1-5
2 3 joe blue 1-5
3 6 john green 6-10
4 3 liz yellow 1-5
5 15 jon pink 11-15





share|improve this answer































    2














    We can use smart_cut from package cutr :



    # devtools::install_github("moodymudskipper/cutr")
    library(cutr)


    Using @Andrie's sample data:



    x$bins <- smart_cut(x$rank,
    c(1,5,11,16),
    labels = ~paste0(.y[1],'-',.y[2]-1),
    simplify = FALSE)
    # rank name info bins
    # 1 1 steve red 1-4
    # 2 3 joe blue 1-4
    # 3 6 john green 5-10
    # 4 3 liz yellow 1-4
    # 5 15 jon pink 11-15


    more on cutr and smart_cut






    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%2f5570293%2fadd-column-which-contains-binned-values-of-an-integer-column%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      42














      See ?cut and specify breaks (and maybe labels).



      x$bins <- cut(x$rank, breaks=c(0,4,10,15), labels=c("1-4","5-10","10-15"))
      x
      # rank name info bins
      # 1 1 steve red 1-4
      # 2 3 joe blue 1-4
      # 3 6 john green 5-10
      # 4 3 liz yellow 1-4
      # 5 15 jon pink 10-15





      share|improve this answer
























      • how can be do it using for loop and assign like function for multiple columns in one go?

        – I_m_LeMarque
        Nov 5 '18 at 15:37
















      42














      See ?cut and specify breaks (and maybe labels).



      x$bins <- cut(x$rank, breaks=c(0,4,10,15), labels=c("1-4","5-10","10-15"))
      x
      # rank name info bins
      # 1 1 steve red 1-4
      # 2 3 joe blue 1-4
      # 3 6 john green 5-10
      # 4 3 liz yellow 1-4
      # 5 15 jon pink 10-15





      share|improve this answer
























      • how can be do it using for loop and assign like function for multiple columns in one go?

        – I_m_LeMarque
        Nov 5 '18 at 15:37














      42












      42








      42







      See ?cut and specify breaks (and maybe labels).



      x$bins <- cut(x$rank, breaks=c(0,4,10,15), labels=c("1-4","5-10","10-15"))
      x
      # rank name info bins
      # 1 1 steve red 1-4
      # 2 3 joe blue 1-4
      # 3 6 john green 5-10
      # 4 3 liz yellow 1-4
      # 5 15 jon pink 10-15





      share|improve this answer













      See ?cut and specify breaks (and maybe labels).



      x$bins <- cut(x$rank, breaks=c(0,4,10,15), labels=c("1-4","5-10","10-15"))
      x
      # rank name info bins
      # 1 1 steve red 1-4
      # 2 3 joe blue 1-4
      # 3 6 john green 5-10
      # 4 3 liz yellow 1-4
      # 5 15 jon pink 10-15






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 6 '11 at 17:17









      Joshua UlrichJoshua Ulrich

      140k23276361




      140k23276361













      • how can be do it using for loop and assign like function for multiple columns in one go?

        – I_m_LeMarque
        Nov 5 '18 at 15:37



















      • how can be do it using for loop and assign like function for multiple columns in one go?

        – I_m_LeMarque
        Nov 5 '18 at 15:37

















      how can be do it using for loop and assign like function for multiple columns in one go?

      – I_m_LeMarque
      Nov 5 '18 at 15:37





      how can be do it using for loop and assign like function for multiple columns in one go?

      – I_m_LeMarque
      Nov 5 '18 at 15:37













      6














      dat <- "rank,name,info
      1,steve,red
      3,joe,blue
      6,john,green
      3,liz,yellow
      15,jon,pink"

      x <- read.table(textConnection(dat), header=TRUE, sep=",", stringsAsFactors=FALSE)
      x$bins <- cut(x$rank, breaks=seq(0, 20, 5), labels=c("1-5", "6-10", "11-15", "16-20"))
      x

      rank name info bins
      1 1 steve red 1-5
      2 3 joe blue 1-5
      3 6 john green 6-10
      4 3 liz yellow 1-5
      5 15 jon pink 11-15





      share|improve this answer




























        6














        dat <- "rank,name,info
        1,steve,red
        3,joe,blue
        6,john,green
        3,liz,yellow
        15,jon,pink"

        x <- read.table(textConnection(dat), header=TRUE, sep=",", stringsAsFactors=FALSE)
        x$bins <- cut(x$rank, breaks=seq(0, 20, 5), labels=c("1-5", "6-10", "11-15", "16-20"))
        x

        rank name info bins
        1 1 steve red 1-5
        2 3 joe blue 1-5
        3 6 john green 6-10
        4 3 liz yellow 1-5
        5 15 jon pink 11-15





        share|improve this answer


























          6












          6








          6







          dat <- "rank,name,info
          1,steve,red
          3,joe,blue
          6,john,green
          3,liz,yellow
          15,jon,pink"

          x <- read.table(textConnection(dat), header=TRUE, sep=",", stringsAsFactors=FALSE)
          x$bins <- cut(x$rank, breaks=seq(0, 20, 5), labels=c("1-5", "6-10", "11-15", "16-20"))
          x

          rank name info bins
          1 1 steve red 1-5
          2 3 joe blue 1-5
          3 6 john green 6-10
          4 3 liz yellow 1-5
          5 15 jon pink 11-15





          share|improve this answer













          dat <- "rank,name,info
          1,steve,red
          3,joe,blue
          6,john,green
          3,liz,yellow
          15,jon,pink"

          x <- read.table(textConnection(dat), header=TRUE, sep=",", stringsAsFactors=FALSE)
          x$bins <- cut(x$rank, breaks=seq(0, 20, 5), labels=c("1-5", "6-10", "11-15", "16-20"))
          x

          rank name info bins
          1 1 steve red 1-5
          2 3 joe blue 1-5
          3 6 john green 6-10
          4 3 liz yellow 1-5
          5 15 jon pink 11-15






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 6 '11 at 17:19









          AndrieAndrie

          137k28358438




          137k28358438























              2














              We can use smart_cut from package cutr :



              # devtools::install_github("moodymudskipper/cutr")
              library(cutr)


              Using @Andrie's sample data:



              x$bins <- smart_cut(x$rank,
              c(1,5,11,16),
              labels = ~paste0(.y[1],'-',.y[2]-1),
              simplify = FALSE)
              # rank name info bins
              # 1 1 steve red 1-4
              # 2 3 joe blue 1-4
              # 3 6 john green 5-10
              # 4 3 liz yellow 1-4
              # 5 15 jon pink 11-15


              more on cutr and smart_cut






              share|improve this answer




























                2














                We can use smart_cut from package cutr :



                # devtools::install_github("moodymudskipper/cutr")
                library(cutr)


                Using @Andrie's sample data:



                x$bins <- smart_cut(x$rank,
                c(1,5,11,16),
                labels = ~paste0(.y[1],'-',.y[2]-1),
                simplify = FALSE)
                # rank name info bins
                # 1 1 steve red 1-4
                # 2 3 joe blue 1-4
                # 3 6 john green 5-10
                # 4 3 liz yellow 1-4
                # 5 15 jon pink 11-15


                more on cutr and smart_cut






                share|improve this answer


























                  2












                  2








                  2







                  We can use smart_cut from package cutr :



                  # devtools::install_github("moodymudskipper/cutr")
                  library(cutr)


                  Using @Andrie's sample data:



                  x$bins <- smart_cut(x$rank,
                  c(1,5,11,16),
                  labels = ~paste0(.y[1],'-',.y[2]-1),
                  simplify = FALSE)
                  # rank name info bins
                  # 1 1 steve red 1-4
                  # 2 3 joe blue 1-4
                  # 3 6 john green 5-10
                  # 4 3 liz yellow 1-4
                  # 5 15 jon pink 11-15


                  more on cutr and smart_cut






                  share|improve this answer













                  We can use smart_cut from package cutr :



                  # devtools::install_github("moodymudskipper/cutr")
                  library(cutr)


                  Using @Andrie's sample data:



                  x$bins <- smart_cut(x$rank,
                  c(1,5,11,16),
                  labels = ~paste0(.y[1],'-',.y[2]-1),
                  simplify = FALSE)
                  # rank name info bins
                  # 1 1 steve red 1-4
                  # 2 3 joe blue 1-4
                  # 3 6 john green 5-10
                  # 4 3 liz yellow 1-4
                  # 5 15 jon pink 11-15


                  more on cutr and smart_cut







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 5 '18 at 22:53









                  Moody_MudskipperMoody_Mudskipper

                  23.8k33365




                  23.8k33365






























                      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%2f5570293%2fadd-column-which-contains-binned-values-of-an-integer-column%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas