Recoding Values For a Single Column in R (includes NULL values)












0















I have a dataframe named data.comp and within it a column named RELAFFIL. The values within the column range from 22 to 107, but there are also numerous NULL values. I wanted to recode the values to 0 for all NULL values, and 1 for all other cases. I have tried multiple approaches and none have worked, FYI I list the ones I've tried below. Also FYI I have the following packages loaded: dplyr, readr, and car.



data.comp$RELAFFIL <- with(data.comp, ifelse(is.null(data.comp$RELAFFIL), 0, 1))

data.comp$RELAFFIL[is.null(data.comp$RELAFFIL)] <- 0

data.comp$RELAFFIL[is.finite(data.comp$RELAFFIL)] <- 1

car::recode(data.comp$RELAFFIL, "NULL = 0; else = 1")

data.comp$RELAFFIL <- data.comp$RELAFFIL %>% base::ifelse(is.null(data.comp$RELAFFIL), 0, 1)









share|improve this question























  • Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

    – Ronak Shah
    Nov 22 '18 at 2:45











  • Does data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1) not work?

    – RAB
    Nov 22 '18 at 3:16











  • No, it recodes all values (including NULL) to 1.

    – Kornel Krysa
    Nov 22 '18 at 3:41
















0















I have a dataframe named data.comp and within it a column named RELAFFIL. The values within the column range from 22 to 107, but there are also numerous NULL values. I wanted to recode the values to 0 for all NULL values, and 1 for all other cases. I have tried multiple approaches and none have worked, FYI I list the ones I've tried below. Also FYI I have the following packages loaded: dplyr, readr, and car.



data.comp$RELAFFIL <- with(data.comp, ifelse(is.null(data.comp$RELAFFIL), 0, 1))

data.comp$RELAFFIL[is.null(data.comp$RELAFFIL)] <- 0

data.comp$RELAFFIL[is.finite(data.comp$RELAFFIL)] <- 1

car::recode(data.comp$RELAFFIL, "NULL = 0; else = 1")

data.comp$RELAFFIL <- data.comp$RELAFFIL %>% base::ifelse(is.null(data.comp$RELAFFIL), 0, 1)









share|improve this question























  • Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

    – Ronak Shah
    Nov 22 '18 at 2:45











  • Does data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1) not work?

    – RAB
    Nov 22 '18 at 3:16











  • No, it recodes all values (including NULL) to 1.

    – Kornel Krysa
    Nov 22 '18 at 3:41














0












0








0








I have a dataframe named data.comp and within it a column named RELAFFIL. The values within the column range from 22 to 107, but there are also numerous NULL values. I wanted to recode the values to 0 for all NULL values, and 1 for all other cases. I have tried multiple approaches and none have worked, FYI I list the ones I've tried below. Also FYI I have the following packages loaded: dplyr, readr, and car.



data.comp$RELAFFIL <- with(data.comp, ifelse(is.null(data.comp$RELAFFIL), 0, 1))

data.comp$RELAFFIL[is.null(data.comp$RELAFFIL)] <- 0

data.comp$RELAFFIL[is.finite(data.comp$RELAFFIL)] <- 1

car::recode(data.comp$RELAFFIL, "NULL = 0; else = 1")

data.comp$RELAFFIL <- data.comp$RELAFFIL %>% base::ifelse(is.null(data.comp$RELAFFIL), 0, 1)









share|improve this question














I have a dataframe named data.comp and within it a column named RELAFFIL. The values within the column range from 22 to 107, but there are also numerous NULL values. I wanted to recode the values to 0 for all NULL values, and 1 for all other cases. I have tried multiple approaches and none have worked, FYI I list the ones I've tried below. Also FYI I have the following packages loaded: dplyr, readr, and car.



data.comp$RELAFFIL <- with(data.comp, ifelse(is.null(data.comp$RELAFFIL), 0, 1))

data.comp$RELAFFIL[is.null(data.comp$RELAFFIL)] <- 0

data.comp$RELAFFIL[is.finite(data.comp$RELAFFIL)] <- 1

car::recode(data.comp$RELAFFIL, "NULL = 0; else = 1")

data.comp$RELAFFIL <- data.comp$RELAFFIL %>% base::ifelse(is.null(data.comp$RELAFFIL), 0, 1)






r null rstudio






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:40









Kornel KrysaKornel Krysa

1




1













  • Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

    – Ronak Shah
    Nov 22 '18 at 2:45











  • Does data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1) not work?

    – RAB
    Nov 22 '18 at 3:16











  • No, it recodes all values (including NULL) to 1.

    – Kornel Krysa
    Nov 22 '18 at 3:41



















  • Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

    – Ronak Shah
    Nov 22 '18 at 2:45











  • Does data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1) not work?

    – RAB
    Nov 22 '18 at 3:16











  • No, it recodes all values (including NULL) to 1.

    – Kornel Krysa
    Nov 22 '18 at 3:41

















Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

– Ronak Shah
Nov 22 '18 at 2:45





Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

– Ronak Shah
Nov 22 '18 at 2:45













Does data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1) not work?

– RAB
Nov 22 '18 at 3:16





Does data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1) not work?

– RAB
Nov 22 '18 at 3:16













No, it recodes all values (including NULL) to 1.

– Kornel Krysa
Nov 22 '18 at 3:41





No, it recodes all values (including NULL) to 1.

– Kornel Krysa
Nov 22 '18 at 3:41












1 Answer
1






active

oldest

votes


















0














Base R solution for this is to improt file mentioning NA strings as null. code is provided below



data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
data.comp[is.na(data.comp)] <- 0

data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)


this will give your required output






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%2f53423139%2frecoding-values-for-a-single-column-in-r-includes-null-values%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














    Base R solution for this is to improt file mentioning NA strings as null. code is provided below



    data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
    data.comp[is.na(data.comp)] <- 0

    data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)


    this will give your required output






    share|improve this answer




























      0














      Base R solution for this is to improt file mentioning NA strings as null. code is provided below



      data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
      data.comp[is.na(data.comp)] <- 0

      data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)


      this will give your required output






      share|improve this answer


























        0












        0








        0







        Base R solution for this is to improt file mentioning NA strings as null. code is provided below



        data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
        data.comp[is.na(data.comp)] <- 0

        data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)


        this will give your required output






        share|improve this answer













        Base R solution for this is to improt file mentioning NA strings as null. code is provided below



        data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
        data.comp[is.na(data.comp)] <- 0

        data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)


        this will give your required output







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 4:08









        HunaidkhanHunaidkhan

        806113




        806113






























            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%2f53423139%2frecoding-values-for-a-single-column-in-r-includes-null-values%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python