Using R how do I change the data in a Column from a Letter to a Word










0















I have a column in my spreadsheet called CourseType and the value in the column is either O, OS or C. I want to change the letters to read Online, Onsite.



How would I do this using R?



Thanks
Paul










share|improve this question






















  • Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)

    – R Yoda
    Nov 15 '18 at 18:05











  • Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?

    – R Yoda
    Nov 15 '18 at 18:08















0















I have a column in my spreadsheet called CourseType and the value in the column is either O, OS or C. I want to change the letters to read Online, Onsite.



How would I do this using R?



Thanks
Paul










share|improve this question






















  • Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)

    – R Yoda
    Nov 15 '18 at 18:05











  • Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?

    – R Yoda
    Nov 15 '18 at 18:08













0












0








0








I have a column in my spreadsheet called CourseType and the value in the column is either O, OS or C. I want to change the letters to read Online, Onsite.



How would I do this using R?



Thanks
Paul










share|improve this question














I have a column in my spreadsheet called CourseType and the value in the column is either O, OS or C. I want to change the letters to read Online, Onsite.



How would I do this using R?



Thanks
Paul







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 16:40









Paul RameshPaul Ramesh

12




12












  • Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)

    – R Yoda
    Nov 15 '18 at 18:05











  • Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?

    – R Yoda
    Nov 15 '18 at 18:08

















  • Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)

    – R Yoda
    Nov 15 '18 at 18:05











  • Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?

    – R Yoda
    Nov 15 '18 at 18:08
















Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)

– R Yoda
Nov 15 '18 at 18:05





Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)

– R Yoda
Nov 15 '18 at 18:05













Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?

– R Yoda
Nov 15 '18 at 18:08





Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?

– R Yoda
Nov 15 '18 at 18:08












2 Answers
2






active

oldest

votes


















2














Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:



df$CourseType_new <- ifelse(df$CourseType == "O", "Online", 
ifelse(df$CourseType == "OS", "OnSite", "something else"))


If you have more values, I'd suggest using a lookup table






share|improve this answer























  • Thanks, I will try this.

    – Paul Ramesh
    Nov 15 '18 at 17:07


















0














If you want to avoid ifelse cascades (e. g. in case of many different codes) you can use this approach:



# Construct example data
df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
df
# CourseType teacher
# 1 C Joe
# 2 O Jane
# 3 O Joe
# 4 OS Jane
# 5 OS Joe
# 6 C Jane

# Do recode by filtering the lines
df$CourseType[df$CourseType == "O"] <- "Online"
df$CourseType[df$CourseType == "OS"] <- "OnSite"

df
# CourseType teacher
# 1 C Joe
# 2 Online Jane
# 3 Online Joe
# 4 OnSite Jane
# 5 OnSite Joe
# 6 C Jane

# Note: The non-matching CourseTypes are left unchanged!


One drawback is that you have no else part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame by adding the recoded value to a new column:



df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"

df
# CourseType teacher CourseTypeRecoded
# 1 C Joe <NA>
# 2 O Jane Online
# 3 O Joe Online
# 4 OS Jane OnSite
# 5 OS Joe OnSite
# 6 C Jane <NA>


You could then set the NA values (which indicate an recode failure) to a default value, e. g.



df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"





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%2f53324083%2fusing-r-how-do-i-change-the-data-in-a-column-from-a-letter-to-a-word%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:



    df$CourseType_new <- ifelse(df$CourseType == "O", "Online", 
    ifelse(df$CourseType == "OS", "OnSite", "something else"))


    If you have more values, I'd suggest using a lookup table






    share|improve this answer























    • Thanks, I will try this.

      – Paul Ramesh
      Nov 15 '18 at 17:07















    2














    Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:



    df$CourseType_new <- ifelse(df$CourseType == "O", "Online", 
    ifelse(df$CourseType == "OS", "OnSite", "something else"))


    If you have more values, I'd suggest using a lookup table






    share|improve this answer























    • Thanks, I will try this.

      – Paul Ramesh
      Nov 15 '18 at 17:07













    2












    2








    2







    Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:



    df$CourseType_new <- ifelse(df$CourseType == "O", "Online", 
    ifelse(df$CourseType == "OS", "OnSite", "something else"))


    If you have more values, I'd suggest using a lookup table






    share|improve this answer













    Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:



    df$CourseType_new <- ifelse(df$CourseType == "O", "Online", 
    ifelse(df$CourseType == "OS", "OnSite", "something else"))


    If you have more values, I'd suggest using a lookup table







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 16:47









    SmitMSmitM

    1,0961211




    1,0961211












    • Thanks, I will try this.

      – Paul Ramesh
      Nov 15 '18 at 17:07

















    • Thanks, I will try this.

      – Paul Ramesh
      Nov 15 '18 at 17:07
















    Thanks, I will try this.

    – Paul Ramesh
    Nov 15 '18 at 17:07





    Thanks, I will try this.

    – Paul Ramesh
    Nov 15 '18 at 17:07













    0














    If you want to avoid ifelse cascades (e. g. in case of many different codes) you can use this approach:



    # Construct example data
    df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
    df
    # CourseType teacher
    # 1 C Joe
    # 2 O Jane
    # 3 O Joe
    # 4 OS Jane
    # 5 OS Joe
    # 6 C Jane

    # Do recode by filtering the lines
    df$CourseType[df$CourseType == "O"] <- "Online"
    df$CourseType[df$CourseType == "OS"] <- "OnSite"

    df
    # CourseType teacher
    # 1 C Joe
    # 2 Online Jane
    # 3 Online Joe
    # 4 OnSite Jane
    # 5 OnSite Joe
    # 6 C Jane

    # Note: The non-matching CourseTypes are left unchanged!


    One drawback is that you have no else part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame by adding the recoded value to a new column:



    df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
    df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"

    df
    # CourseType teacher CourseTypeRecoded
    # 1 C Joe <NA>
    # 2 O Jane Online
    # 3 O Joe Online
    # 4 OS Jane OnSite
    # 5 OS Joe OnSite
    # 6 C Jane <NA>


    You could then set the NA values (which indicate an recode failure) to a default value, e. g.



    df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"





    share|improve this answer





























      0














      If you want to avoid ifelse cascades (e. g. in case of many different codes) you can use this approach:



      # Construct example data
      df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
      df
      # CourseType teacher
      # 1 C Joe
      # 2 O Jane
      # 3 O Joe
      # 4 OS Jane
      # 5 OS Joe
      # 6 C Jane

      # Do recode by filtering the lines
      df$CourseType[df$CourseType == "O"] <- "Online"
      df$CourseType[df$CourseType == "OS"] <- "OnSite"

      df
      # CourseType teacher
      # 1 C Joe
      # 2 Online Jane
      # 3 Online Joe
      # 4 OnSite Jane
      # 5 OnSite Joe
      # 6 C Jane

      # Note: The non-matching CourseTypes are left unchanged!


      One drawback is that you have no else part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame by adding the recoded value to a new column:



      df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
      df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"

      df
      # CourseType teacher CourseTypeRecoded
      # 1 C Joe <NA>
      # 2 O Jane Online
      # 3 O Joe Online
      # 4 OS Jane OnSite
      # 5 OS Joe OnSite
      # 6 C Jane <NA>


      You could then set the NA values (which indicate an recode failure) to a default value, e. g.



      df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"





      share|improve this answer



























        0












        0








        0







        If you want to avoid ifelse cascades (e. g. in case of many different codes) you can use this approach:



        # Construct example data
        df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
        df
        # CourseType teacher
        # 1 C Joe
        # 2 O Jane
        # 3 O Joe
        # 4 OS Jane
        # 5 OS Joe
        # 6 C Jane

        # Do recode by filtering the lines
        df$CourseType[df$CourseType == "O"] <- "Online"
        df$CourseType[df$CourseType == "OS"] <- "OnSite"

        df
        # CourseType teacher
        # 1 C Joe
        # 2 Online Jane
        # 3 Online Joe
        # 4 OnSite Jane
        # 5 OnSite Joe
        # 6 C Jane

        # Note: The non-matching CourseTypes are left unchanged!


        One drawback is that you have no else part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame by adding the recoded value to a new column:



        df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
        df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"

        df
        # CourseType teacher CourseTypeRecoded
        # 1 C Joe <NA>
        # 2 O Jane Online
        # 3 O Joe Online
        # 4 OS Jane OnSite
        # 5 OS Joe OnSite
        # 6 C Jane <NA>


        You could then set the NA values (which indicate an recode failure) to a default value, e. g.



        df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"





        share|improve this answer















        If you want to avoid ifelse cascades (e. g. in case of many different codes) you can use this approach:



        # Construct example data
        df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
        df
        # CourseType teacher
        # 1 C Joe
        # 2 O Jane
        # 3 O Joe
        # 4 OS Jane
        # 5 OS Joe
        # 6 C Jane

        # Do recode by filtering the lines
        df$CourseType[df$CourseType == "O"] <- "Online"
        df$CourseType[df$CourseType == "OS"] <- "OnSite"

        df
        # CourseType teacher
        # 1 C Joe
        # 2 Online Jane
        # 3 Online Joe
        # 4 OnSite Jane
        # 5 OnSite Joe
        # 6 C Jane

        # Note: The non-matching CourseTypes are left unchanged!


        One drawback is that you have no else part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame by adding the recoded value to a new column:



        df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
        df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"

        df
        # CourseType teacher CourseTypeRecoded
        # 1 C Joe <NA>
        # 2 O Jane Online
        # 3 O Joe Online
        # 4 OS Jane OnSite
        # 5 OS Joe OnSite
        # 6 C Jane <NA>


        You could then set the NA values (which indicate an recode failure) to a default value, e. g.



        df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 18:53

























        answered Nov 15 '18 at 18:47









        R YodaR Yoda

        4,2532041




        4,2532041



























            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%2f53324083%2fusing-r-how-do-i-change-the-data-in-a-column-from-a-letter-to-a-word%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線