R:How to replace string to integer?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I have a dataset looks like:



classification  Interest    Age     Gender
Card battle IL029 18-24 male
Card battle IL001 45-54 male
Card battle IL001 18-24 male
Card battle IL001 35-44 male
Card battle IL001 35-44 male
Card battle IL013 35-44 male


How to replace "18-24" to 20,"35-44" to 40 and "45-54" to 50 in the age column?










share|improve this question























  • Have you thought about what you would like to replace them with?

    – r.bot
    May 18 '15 at 8:55






  • 2





    Not sure if you what's the desired output, but you could do factor(df$Age, labels = c("20", "40", "50"))

    – David Arenburg
    May 18 '15 at 9:05


















2















I have a dataset looks like:



classification  Interest    Age     Gender
Card battle IL029 18-24 male
Card battle IL001 45-54 male
Card battle IL001 18-24 male
Card battle IL001 35-44 male
Card battle IL001 35-44 male
Card battle IL013 35-44 male


How to replace "18-24" to 20,"35-44" to 40 and "45-54" to 50 in the age column?










share|improve this question























  • Have you thought about what you would like to replace them with?

    – r.bot
    May 18 '15 at 8:55






  • 2





    Not sure if you what's the desired output, but you could do factor(df$Age, labels = c("20", "40", "50"))

    – David Arenburg
    May 18 '15 at 9:05














2












2








2








I have a dataset looks like:



classification  Interest    Age     Gender
Card battle IL029 18-24 male
Card battle IL001 45-54 male
Card battle IL001 18-24 male
Card battle IL001 35-44 male
Card battle IL001 35-44 male
Card battle IL013 35-44 male


How to replace "18-24" to 20,"35-44" to 40 and "45-54" to 50 in the age column?










share|improve this question














I have a dataset looks like:



classification  Interest    Age     Gender
Card battle IL029 18-24 male
Card battle IL001 45-54 male
Card battle IL001 18-24 male
Card battle IL001 35-44 male
Card battle IL001 35-44 male
Card battle IL013 35-44 male


How to replace "18-24" to 20,"35-44" to 40 and "45-54" to 50 in the age column?







r replace






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 18 '15 at 8:51









Jeffery ChenJeffery Chen

163210




163210













  • Have you thought about what you would like to replace them with?

    – r.bot
    May 18 '15 at 8:55






  • 2





    Not sure if you what's the desired output, but you could do factor(df$Age, labels = c("20", "40", "50"))

    – David Arenburg
    May 18 '15 at 9:05



















  • Have you thought about what you would like to replace them with?

    – r.bot
    May 18 '15 at 8:55






  • 2





    Not sure if you what's the desired output, but you could do factor(df$Age, labels = c("20", "40", "50"))

    – David Arenburg
    May 18 '15 at 9:05

















Have you thought about what you would like to replace them with?

– r.bot
May 18 '15 at 8:55





Have you thought about what you would like to replace them with?

– r.bot
May 18 '15 at 8:55




2




2





Not sure if you what's the desired output, but you could do factor(df$Age, labels = c("20", "40", "50"))

– David Arenburg
May 18 '15 at 9:05





Not sure if you what's the desired output, but you could do factor(df$Age, labels = c("20", "40", "50"))

– David Arenburg
May 18 '15 at 9:05












4 Answers
4






active

oldest

votes


















4














Try something like this



data$age <- as.character(data$age)
data$age[which(data$age=="18-24")] <- "20"
data$age[which(data$age=="35-44")] <- "40"
data$age[which(data$age=="45-54")] <- "50"
data$age <- as.numeric(data$age)





share|improve this answer































    4














    This will replace Age with a factor having labels 20, 40 and 50:



    transform(DF, Age = factor(Age, 
    levels = c("18-24", "35-44", "45-54"),
    labels = c(20, 40, 50)))


    giving:



      classification Interest Age Gender
    1 Card battle IL029 20 male
    2 Card battle IL001 50 male
    3 Card battle IL001 20 male
    4 Card battle IL001 40 male
    5 Card battle IL001 40 male
    6 Card battle IL013 40 male


    Actually it can likely be reduced to this although the above is a bit safer:



    transform(DF, Age = factor(Age, labels = c(20, 40, 50)))


    If you prefer an integer column then:



    transform(DF, Age = as.integer(as.character(
    factor(Age,
    levels = c("18-24", "35-44", "45-54"),
    labels = c(20, 40, 50)
    )
    )))


    and, again, we could likely omit the levels argument:



    transform(DF, Age = as.integer(as.character(factor(Age, labels = c(20, 40, 50)))))


    Note: We used this as input:



    DF <-
    structure(list(classification = structure(c(1L, 1L, 1L, 1L, 1L,
    1L), .Label = "Card battle", class = "factor"), Interest = structure(c(3L,
    1L, 1L, 1L, 1L, 2L), .Label = c("IL001", "IL013", "IL029"), class = "factor"),
    Age = structure(c(1L, 3L, 1L, 2L, 2L, 2L), .Label = c("18-24",
    "35-44", "45-54"), class = "factor"), Gender = structure(c(1L,
    1L, 1L, 1L, 1L, 1L), .Label = "male", class = "factor")), .Names = c("classification",
    "Interest", "Age", "Gender"), class = "data.frame", row.names = c(NA,
    -6L))





    share|improve this answer


























    • Didn't notice that but at any rate note that your comment might not work depending on the mapping of factor levels to labels whereas the answer here should work.

      – G. Grothendieck
      May 18 '15 at 19:32



















    2














    A data.table solution is to merge (much easier to extend to more complicated cases):



    library(data.table)
    #your data
    DT = data.table(
    classification = "Card battle",
    Interest = sprintf('IL%03d', c(29, 1, 1, 1, 1, 13)),
    Age = c("18-24","45-54","18-24", rep("35-44", 3L)),
    Gender = "male"
    )

    #conversion table
    convert = data.table(
    Age_range = c("18-24", "45-54", "35-44"),
    #need to keep as string here since
    # the target column to overwrite is character
    Age_middle = paste0(c(20, 40, 50))
    )

    #replace Age, then set its class
    DT[convert, on = c(Age = 'Age_range'), Age := i.Age_middle]
    # now convert back to numeric
    DT[ , Age := as.numeric(Age)]


    You might consider keeping the range column around, and simply adding a rounded age column, which would make for cleaner code:



    convert = data.table(
    Age_range = c("18-24","45-54","35-44"),
    Age_middle = c(20L,40L,50L)
    )

    DT[convert, Age_middle := i.Age_middle]
    DT
    # classification Interest Age Gender age_rounded
    # 1: Card battle IL029 18-24 male 20
    # 2: Card battle IL001 18-24 male 20
    # 3: Card battle IL001 35-44 male 50
    # 4: Card battle IL001 35-44 male 50
    # 5: Card battle IL013 35-44 male 50
    # 6: Card battle IL001 45-54 male 40





    share|improve this answer

































      0














      Another way, using regex, capturing the second to last digit and putting a 0 after:



      DF$Age <- as.numeric(sub(".*(\d)\d$", "\10", as.character(DF$Age)))


      (or simply as.numeric(sub(".*(\d)\d$", "\10", DF$Age)) if Age is not a factor)



      DF
      # classification Interest Age Gender
      #1 Card battle IL029 20 male
      #2 Card battle IL001 50 male
      #3 Card battle IL001 20 male
      #4 Card battle IL001 40 male
      #5 Card battle IL001 40 male
      #6 Card battle IL013 40 male





      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%2f30298773%2frhow-to-replace-string-to-integer%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Try something like this



        data$age <- as.character(data$age)
        data$age[which(data$age=="18-24")] <- "20"
        data$age[which(data$age=="35-44")] <- "40"
        data$age[which(data$age=="45-54")] <- "50"
        data$age <- as.numeric(data$age)





        share|improve this answer




























          4














          Try something like this



          data$age <- as.character(data$age)
          data$age[which(data$age=="18-24")] <- "20"
          data$age[which(data$age=="35-44")] <- "40"
          data$age[which(data$age=="45-54")] <- "50"
          data$age <- as.numeric(data$age)





          share|improve this answer


























            4












            4








            4







            Try something like this



            data$age <- as.character(data$age)
            data$age[which(data$age=="18-24")] <- "20"
            data$age[which(data$age=="35-44")] <- "40"
            data$age[which(data$age=="45-54")] <- "50"
            data$age <- as.numeric(data$age)





            share|improve this answer













            Try something like this



            data$age <- as.character(data$age)
            data$age[which(data$age=="18-24")] <- "20"
            data$age[which(data$age=="35-44")] <- "40"
            data$age[which(data$age=="45-54")] <- "50"
            data$age <- as.numeric(data$age)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 18 '15 at 9:00









            Fedorenko KristinaFedorenko Kristina

            1,21711216




            1,21711216

























                4














                This will replace Age with a factor having labels 20, 40 and 50:



                transform(DF, Age = factor(Age, 
                levels = c("18-24", "35-44", "45-54"),
                labels = c(20, 40, 50)))


                giving:



                  classification Interest Age Gender
                1 Card battle IL029 20 male
                2 Card battle IL001 50 male
                3 Card battle IL001 20 male
                4 Card battle IL001 40 male
                5 Card battle IL001 40 male
                6 Card battle IL013 40 male


                Actually it can likely be reduced to this although the above is a bit safer:



                transform(DF, Age = factor(Age, labels = c(20, 40, 50)))


                If you prefer an integer column then:



                transform(DF, Age = as.integer(as.character(
                factor(Age,
                levels = c("18-24", "35-44", "45-54"),
                labels = c(20, 40, 50)
                )
                )))


                and, again, we could likely omit the levels argument:



                transform(DF, Age = as.integer(as.character(factor(Age, labels = c(20, 40, 50)))))


                Note: We used this as input:



                DF <-
                structure(list(classification = structure(c(1L, 1L, 1L, 1L, 1L,
                1L), .Label = "Card battle", class = "factor"), Interest = structure(c(3L,
                1L, 1L, 1L, 1L, 2L), .Label = c("IL001", "IL013", "IL029"), class = "factor"),
                Age = structure(c(1L, 3L, 1L, 2L, 2L, 2L), .Label = c("18-24",
                "35-44", "45-54"), class = "factor"), Gender = structure(c(1L,
                1L, 1L, 1L, 1L, 1L), .Label = "male", class = "factor")), .Names = c("classification",
                "Interest", "Age", "Gender"), class = "data.frame", row.names = c(NA,
                -6L))





                share|improve this answer


























                • Didn't notice that but at any rate note that your comment might not work depending on the mapping of factor levels to labels whereas the answer here should work.

                  – G. Grothendieck
                  May 18 '15 at 19:32
















                4














                This will replace Age with a factor having labels 20, 40 and 50:



                transform(DF, Age = factor(Age, 
                levels = c("18-24", "35-44", "45-54"),
                labels = c(20, 40, 50)))


                giving:



                  classification Interest Age Gender
                1 Card battle IL029 20 male
                2 Card battle IL001 50 male
                3 Card battle IL001 20 male
                4 Card battle IL001 40 male
                5 Card battle IL001 40 male
                6 Card battle IL013 40 male


                Actually it can likely be reduced to this although the above is a bit safer:



                transform(DF, Age = factor(Age, labels = c(20, 40, 50)))


                If you prefer an integer column then:



                transform(DF, Age = as.integer(as.character(
                factor(Age,
                levels = c("18-24", "35-44", "45-54"),
                labels = c(20, 40, 50)
                )
                )))


                and, again, we could likely omit the levels argument:



                transform(DF, Age = as.integer(as.character(factor(Age, labels = c(20, 40, 50)))))


                Note: We used this as input:



                DF <-
                structure(list(classification = structure(c(1L, 1L, 1L, 1L, 1L,
                1L), .Label = "Card battle", class = "factor"), Interest = structure(c(3L,
                1L, 1L, 1L, 1L, 2L), .Label = c("IL001", "IL013", "IL029"), class = "factor"),
                Age = structure(c(1L, 3L, 1L, 2L, 2L, 2L), .Label = c("18-24",
                "35-44", "45-54"), class = "factor"), Gender = structure(c(1L,
                1L, 1L, 1L, 1L, 1L), .Label = "male", class = "factor")), .Names = c("classification",
                "Interest", "Age", "Gender"), class = "data.frame", row.names = c(NA,
                -6L))





                share|improve this answer


























                • Didn't notice that but at any rate note that your comment might not work depending on the mapping of factor levels to labels whereas the answer here should work.

                  – G. Grothendieck
                  May 18 '15 at 19:32














                4












                4








                4







                This will replace Age with a factor having labels 20, 40 and 50:



                transform(DF, Age = factor(Age, 
                levels = c("18-24", "35-44", "45-54"),
                labels = c(20, 40, 50)))


                giving:



                  classification Interest Age Gender
                1 Card battle IL029 20 male
                2 Card battle IL001 50 male
                3 Card battle IL001 20 male
                4 Card battle IL001 40 male
                5 Card battle IL001 40 male
                6 Card battle IL013 40 male


                Actually it can likely be reduced to this although the above is a bit safer:



                transform(DF, Age = factor(Age, labels = c(20, 40, 50)))


                If you prefer an integer column then:



                transform(DF, Age = as.integer(as.character(
                factor(Age,
                levels = c("18-24", "35-44", "45-54"),
                labels = c(20, 40, 50)
                )
                )))


                and, again, we could likely omit the levels argument:



                transform(DF, Age = as.integer(as.character(factor(Age, labels = c(20, 40, 50)))))


                Note: We used this as input:



                DF <-
                structure(list(classification = structure(c(1L, 1L, 1L, 1L, 1L,
                1L), .Label = "Card battle", class = "factor"), Interest = structure(c(3L,
                1L, 1L, 1L, 1L, 2L), .Label = c("IL001", "IL013", "IL029"), class = "factor"),
                Age = structure(c(1L, 3L, 1L, 2L, 2L, 2L), .Label = c("18-24",
                "35-44", "45-54"), class = "factor"), Gender = structure(c(1L,
                1L, 1L, 1L, 1L, 1L), .Label = "male", class = "factor")), .Names = c("classification",
                "Interest", "Age", "Gender"), class = "data.frame", row.names = c(NA,
                -6L))





                share|improve this answer















                This will replace Age with a factor having labels 20, 40 and 50:



                transform(DF, Age = factor(Age, 
                levels = c("18-24", "35-44", "45-54"),
                labels = c(20, 40, 50)))


                giving:



                  classification Interest Age Gender
                1 Card battle IL029 20 male
                2 Card battle IL001 50 male
                3 Card battle IL001 20 male
                4 Card battle IL001 40 male
                5 Card battle IL001 40 male
                6 Card battle IL013 40 male


                Actually it can likely be reduced to this although the above is a bit safer:



                transform(DF, Age = factor(Age, labels = c(20, 40, 50)))


                If you prefer an integer column then:



                transform(DF, Age = as.integer(as.character(
                factor(Age,
                levels = c("18-24", "35-44", "45-54"),
                labels = c(20, 40, 50)
                )
                )))


                and, again, we could likely omit the levels argument:



                transform(DF, Age = as.integer(as.character(factor(Age, labels = c(20, 40, 50)))))


                Note: We used this as input:



                DF <-
                structure(list(classification = structure(c(1L, 1L, 1L, 1L, 1L,
                1L), .Label = "Card battle", class = "factor"), Interest = structure(c(3L,
                1L, 1L, 1L, 1L, 2L), .Label = c("IL001", "IL013", "IL029"), class = "factor"),
                Age = structure(c(1L, 3L, 1L, 2L, 2L, 2L), .Label = c("18-24",
                "35-44", "45-54"), class = "factor"), Gender = structure(c(1L,
                1L, 1L, 1L, 1L, 1L), .Label = "male", class = "factor")), .Names = c("classification",
                "Interest", "Age", "Gender"), class = "data.frame", row.names = c(NA,
                -6L))






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 18 '15 at 13:14

























                answered May 18 '15 at 13:03









                G. GrothendieckG. Grothendieck

                154k11137244




                154k11137244













                • Didn't notice that but at any rate note that your comment might not work depending on the mapping of factor levels to labels whereas the answer here should work.

                  – G. Grothendieck
                  May 18 '15 at 19:32



















                • Didn't notice that but at any rate note that your comment might not work depending on the mapping of factor levels to labels whereas the answer here should work.

                  – G. Grothendieck
                  May 18 '15 at 19:32

















                Didn't notice that but at any rate note that your comment might not work depending on the mapping of factor levels to labels whereas the answer here should work.

                – G. Grothendieck
                May 18 '15 at 19:32





                Didn't notice that but at any rate note that your comment might not work depending on the mapping of factor levels to labels whereas the answer here should work.

                – G. Grothendieck
                May 18 '15 at 19:32











                2














                A data.table solution is to merge (much easier to extend to more complicated cases):



                library(data.table)
                #your data
                DT = data.table(
                classification = "Card battle",
                Interest = sprintf('IL%03d', c(29, 1, 1, 1, 1, 13)),
                Age = c("18-24","45-54","18-24", rep("35-44", 3L)),
                Gender = "male"
                )

                #conversion table
                convert = data.table(
                Age_range = c("18-24", "45-54", "35-44"),
                #need to keep as string here since
                # the target column to overwrite is character
                Age_middle = paste0(c(20, 40, 50))
                )

                #replace Age, then set its class
                DT[convert, on = c(Age = 'Age_range'), Age := i.Age_middle]
                # now convert back to numeric
                DT[ , Age := as.numeric(Age)]


                You might consider keeping the range column around, and simply adding a rounded age column, which would make for cleaner code:



                convert = data.table(
                Age_range = c("18-24","45-54","35-44"),
                Age_middle = c(20L,40L,50L)
                )

                DT[convert, Age_middle := i.Age_middle]
                DT
                # classification Interest Age Gender age_rounded
                # 1: Card battle IL029 18-24 male 20
                # 2: Card battle IL001 18-24 male 20
                # 3: Card battle IL001 35-44 male 50
                # 4: Card battle IL001 35-44 male 50
                # 5: Card battle IL013 35-44 male 50
                # 6: Card battle IL001 45-54 male 40





                share|improve this answer






























                  2














                  A data.table solution is to merge (much easier to extend to more complicated cases):



                  library(data.table)
                  #your data
                  DT = data.table(
                  classification = "Card battle",
                  Interest = sprintf('IL%03d', c(29, 1, 1, 1, 1, 13)),
                  Age = c("18-24","45-54","18-24", rep("35-44", 3L)),
                  Gender = "male"
                  )

                  #conversion table
                  convert = data.table(
                  Age_range = c("18-24", "45-54", "35-44"),
                  #need to keep as string here since
                  # the target column to overwrite is character
                  Age_middle = paste0(c(20, 40, 50))
                  )

                  #replace Age, then set its class
                  DT[convert, on = c(Age = 'Age_range'), Age := i.Age_middle]
                  # now convert back to numeric
                  DT[ , Age := as.numeric(Age)]


                  You might consider keeping the range column around, and simply adding a rounded age column, which would make for cleaner code:



                  convert = data.table(
                  Age_range = c("18-24","45-54","35-44"),
                  Age_middle = c(20L,40L,50L)
                  )

                  DT[convert, Age_middle := i.Age_middle]
                  DT
                  # classification Interest Age Gender age_rounded
                  # 1: Card battle IL029 18-24 male 20
                  # 2: Card battle IL001 18-24 male 20
                  # 3: Card battle IL001 35-44 male 50
                  # 4: Card battle IL001 35-44 male 50
                  # 5: Card battle IL013 35-44 male 50
                  # 6: Card battle IL001 45-54 male 40





                  share|improve this answer




























                    2












                    2








                    2







                    A data.table solution is to merge (much easier to extend to more complicated cases):



                    library(data.table)
                    #your data
                    DT = data.table(
                    classification = "Card battle",
                    Interest = sprintf('IL%03d', c(29, 1, 1, 1, 1, 13)),
                    Age = c("18-24","45-54","18-24", rep("35-44", 3L)),
                    Gender = "male"
                    )

                    #conversion table
                    convert = data.table(
                    Age_range = c("18-24", "45-54", "35-44"),
                    #need to keep as string here since
                    # the target column to overwrite is character
                    Age_middle = paste0(c(20, 40, 50))
                    )

                    #replace Age, then set its class
                    DT[convert, on = c(Age = 'Age_range'), Age := i.Age_middle]
                    # now convert back to numeric
                    DT[ , Age := as.numeric(Age)]


                    You might consider keeping the range column around, and simply adding a rounded age column, which would make for cleaner code:



                    convert = data.table(
                    Age_range = c("18-24","45-54","35-44"),
                    Age_middle = c(20L,40L,50L)
                    )

                    DT[convert, Age_middle := i.Age_middle]
                    DT
                    # classification Interest Age Gender age_rounded
                    # 1: Card battle IL029 18-24 male 20
                    # 2: Card battle IL001 18-24 male 20
                    # 3: Card battle IL001 35-44 male 50
                    # 4: Card battle IL001 35-44 male 50
                    # 5: Card battle IL013 35-44 male 50
                    # 6: Card battle IL001 45-54 male 40





                    share|improve this answer















                    A data.table solution is to merge (much easier to extend to more complicated cases):



                    library(data.table)
                    #your data
                    DT = data.table(
                    classification = "Card battle",
                    Interest = sprintf('IL%03d', c(29, 1, 1, 1, 1, 13)),
                    Age = c("18-24","45-54","18-24", rep("35-44", 3L)),
                    Gender = "male"
                    )

                    #conversion table
                    convert = data.table(
                    Age_range = c("18-24", "45-54", "35-44"),
                    #need to keep as string here since
                    # the target column to overwrite is character
                    Age_middle = paste0(c(20, 40, 50))
                    )

                    #replace Age, then set its class
                    DT[convert, on = c(Age = 'Age_range'), Age := i.Age_middle]
                    # now convert back to numeric
                    DT[ , Age := as.numeric(Age)]


                    You might consider keeping the range column around, and simply adding a rounded age column, which would make for cleaner code:



                    convert = data.table(
                    Age_range = c("18-24","45-54","35-44"),
                    Age_middle = c(20L,40L,50L)
                    )

                    DT[convert, Age_middle := i.Age_middle]
                    DT
                    # classification Interest Age Gender age_rounded
                    # 1: Card battle IL029 18-24 male 20
                    # 2: Card battle IL001 18-24 male 20
                    # 3: Card battle IL001 35-44 male 50
                    # 4: Card battle IL001 35-44 male 50
                    # 5: Card battle IL013 35-44 male 50
                    # 6: Card battle IL001 45-54 male 40






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 23 '18 at 16:29

























                    answered May 18 '15 at 18:18









                    MichaelChiricoMichaelChirico

                    20.7k863118




                    20.7k863118























                        0














                        Another way, using regex, capturing the second to last digit and putting a 0 after:



                        DF$Age <- as.numeric(sub(".*(\d)\d$", "\10", as.character(DF$Age)))


                        (or simply as.numeric(sub(".*(\d)\d$", "\10", DF$Age)) if Age is not a factor)



                        DF
                        # classification Interest Age Gender
                        #1 Card battle IL029 20 male
                        #2 Card battle IL001 50 male
                        #3 Card battle IL001 20 male
                        #4 Card battle IL001 40 male
                        #5 Card battle IL001 40 male
                        #6 Card battle IL013 40 male





                        share|improve this answer




























                          0














                          Another way, using regex, capturing the second to last digit and putting a 0 after:



                          DF$Age <- as.numeric(sub(".*(\d)\d$", "\10", as.character(DF$Age)))


                          (or simply as.numeric(sub(".*(\d)\d$", "\10", DF$Age)) if Age is not a factor)



                          DF
                          # classification Interest Age Gender
                          #1 Card battle IL029 20 male
                          #2 Card battle IL001 50 male
                          #3 Card battle IL001 20 male
                          #4 Card battle IL001 40 male
                          #5 Card battle IL001 40 male
                          #6 Card battle IL013 40 male





                          share|improve this answer


























                            0












                            0








                            0







                            Another way, using regex, capturing the second to last digit and putting a 0 after:



                            DF$Age <- as.numeric(sub(".*(\d)\d$", "\10", as.character(DF$Age)))


                            (or simply as.numeric(sub(".*(\d)\d$", "\10", DF$Age)) if Age is not a factor)



                            DF
                            # classification Interest Age Gender
                            #1 Card battle IL029 20 male
                            #2 Card battle IL001 50 male
                            #3 Card battle IL001 20 male
                            #4 Card battle IL001 40 male
                            #5 Card battle IL001 40 male
                            #6 Card battle IL013 40 male





                            share|improve this answer













                            Another way, using regex, capturing the second to last digit and putting a 0 after:



                            DF$Age <- as.numeric(sub(".*(\d)\d$", "\10", as.character(DF$Age)))


                            (or simply as.numeric(sub(".*(\d)\d$", "\10", DF$Age)) if Age is not a factor)



                            DF
                            # classification Interest Age Gender
                            #1 Card battle IL029 20 male
                            #2 Card battle IL001 50 male
                            #3 Card battle IL001 20 male
                            #4 Card battle IL001 40 male
                            #5 Card battle IL001 40 male
                            #6 Card battle IL013 40 male






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 25 '15 at 11:13









                            CathCath

                            20.2k43766




                            20.2k43766






























                                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%2f30298773%2frhow-to-replace-string-to-integer%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

                                If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                Alcedinidae

                                Can an atomic nucleus contain both particles and antiparticles? [duplicate]