Changing Values in a column based on criteria in R Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I quickly rename a MySQL database (change schema name)?How to sort a dataframe by multiple column(s)How to sum a variable by groupEasiest way to rename a model using Django/South?Renaming columns in pandasCall apply-like function on each row of dataframe with multiple arguments from each rowRename multiple columns by namesHow to rename columns of R dataframe based on some existing pattern?Change values of multiple columns based on the value of one column in data.tableChange column name based on row values

Google .dev domain strangely redirects to https

Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?

What to do with repeated rejections for phd position

An adverb for when you're not exaggerating

What are the discoveries that have been possible with the rejection of positivism?

Did Mueller's report provide an evidentiary basis for the claim of Russian govt election interference via social media?

What does it mean that physics no longer uses mechanical models to describe phenomena?

Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode

Tannaka duality for semisimple groups

A term for a woman complaining about things/begging in a cute/childish way

What is the meaning of 'breadth' in breadth first search?

If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?

Why are my pictures showing a dark band on one edge?

What order were files/directories output in dir?

What does this say in Elvish?

1-probability to calculate two events in a row

What does Turing mean by this statement?

Why weren't discrete x86 CPUs ever used in game hardware?

Flash light on something

What does 丫 mean? 丫是什么意思?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

How to compare two different files line by line in unix?

Getting prompted for verification code but where do I put it in?

How would a mousetrap for use in space work?



Changing Values in a column based on criteria in R



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I quickly rename a MySQL database (change schema name)?How to sort a dataframe by multiple column(s)How to sum a variable by groupEasiest way to rename a model using Django/South?Renaming columns in pandasCall apply-like function on each row of dataframe with multiple arguments from each rowRename multiple columns by namesHow to rename columns of R dataframe based on some existing pattern?Change values of multiple columns based on the value of one column in data.tableChange column name based on row values



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








0















Let's say I have a column data$code with values like 1234, 222F, 456Q, 1234, 1234...
Now I want to rename them so that every occurrence of 1234 would be 'Model 1', every occerence of 456Q -> 'Model X' etc.



I tried to make a for loop but it wouldn't work. Maybe there is an easier way?










share|improve this question






















  • Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask

    – DJV
    Mar 8 at 21:56


















0















Let's say I have a column data$code with values like 1234, 222F, 456Q, 1234, 1234...
Now I want to rename them so that every occurrence of 1234 would be 'Model 1', every occerence of 456Q -> 'Model X' etc.



I tried to make a for loop but it wouldn't work. Maybe there is an easier way?










share|improve this question






















  • Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask

    – DJV
    Mar 8 at 21:56














0












0








0








Let's say I have a column data$code with values like 1234, 222F, 456Q, 1234, 1234...
Now I want to rename them so that every occurrence of 1234 would be 'Model 1', every occerence of 456Q -> 'Model X' etc.



I tried to make a for loop but it wouldn't work. Maybe there is an easier way?










share|improve this question














Let's say I have a column data$code with values like 1234, 222F, 456Q, 1234, 1234...
Now I want to rename them so that every occurrence of 1234 would be 'Model 1', every occerence of 456Q -> 'Model X' etc.



I tried to make a for loop but it wouldn't work. Maybe there is an easier way?







r rename






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 21:53









koraskoras

53




53












  • Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask

    – DJV
    Mar 8 at 21:56


















  • Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask

    – DJV
    Mar 8 at 21:56

















Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask

– DJV
Mar 8 at 21:56






Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask

– DJV
Mar 8 at 21:56













1 Answer
1






active

oldest

votes


















0














from you example I assume that code is a categorical variable, therefore what you need to do is change a value of the levels for that variable.



For example:



id <- c(1,2,3,4,5)
code <- c('1234', '222F', '456Q', '1234', '1234')
data <- data.frame(id, code)
data

id code
1 1 1234
2 2 222F
3 3 456Q
4 4 1234
5 5 1234

levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
data

id code
1 1 Model 1
2 2 222F
3 3 Model X
4 4 Model 1
5 5 Model 1


You simply have to repeat for every level you need to change.






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%2f55071517%2fchanging-values-in-a-column-based-on-criteria-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









    0














    from you example I assume that code is a categorical variable, therefore what you need to do is change a value of the levels for that variable.



    For example:



    id <- c(1,2,3,4,5)
    code <- c('1234', '222F', '456Q', '1234', '1234')
    data <- data.frame(id, code)
    data

    id code
    1 1 1234
    2 2 222F
    3 3 456Q
    4 4 1234
    5 5 1234

    levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
    levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
    data

    id code
    1 1 Model 1
    2 2 222F
    3 3 Model X
    4 4 Model 1
    5 5 Model 1


    You simply have to repeat for every level you need to change.






    share|improve this answer



























      0














      from you example I assume that code is a categorical variable, therefore what you need to do is change a value of the levels for that variable.



      For example:



      id <- c(1,2,3,4,5)
      code <- c('1234', '222F', '456Q', '1234', '1234')
      data <- data.frame(id, code)
      data

      id code
      1 1 1234
      2 2 222F
      3 3 456Q
      4 4 1234
      5 5 1234

      levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
      levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
      data

      id code
      1 1 Model 1
      2 2 222F
      3 3 Model X
      4 4 Model 1
      5 5 Model 1


      You simply have to repeat for every level you need to change.






      share|improve this answer

























        0












        0








        0







        from you example I assume that code is a categorical variable, therefore what you need to do is change a value of the levels for that variable.



        For example:



        id <- c(1,2,3,4,5)
        code <- c('1234', '222F', '456Q', '1234', '1234')
        data <- data.frame(id, code)
        data

        id code
        1 1 1234
        2 2 222F
        3 3 456Q
        4 4 1234
        5 5 1234

        levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
        levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
        data

        id code
        1 1 Model 1
        2 2 222F
        3 3 Model X
        4 4 Model 1
        5 5 Model 1


        You simply have to repeat for every level you need to change.






        share|improve this answer













        from you example I assume that code is a categorical variable, therefore what you need to do is change a value of the levels for that variable.



        For example:



        id <- c(1,2,3,4,5)
        code <- c('1234', '222F', '456Q', '1234', '1234')
        data <- data.frame(id, code)
        data

        id code
        1 1 1234
        2 2 222F
        3 3 456Q
        4 4 1234
        5 5 1234

        levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
        levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
        data

        id code
        1 1 Model 1
        2 2 222F
        3 3 Model X
        4 4 Model 1
        5 5 Model 1


        You simply have to repeat for every level you need to change.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 2:44









        alessioalessio

        37427




        37427





























            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%2f55071517%2fchanging-values-in-a-column-based-on-criteria-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

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved