Replace all particular values in a data frameUsing regex to set a specific digit to NA?Remove Characters in Rparsing quotes out of “NA” stringsReplace specific characters in a variable in data frame in RRegression Analysis in RReplace character in the whole dataframeReplace values in an R data frame in Python using rpy2Join values when creating boxplotHow to replace a character by a newline in Vim?How to replace all occurrences of a string in JavaScriptHow to join (merge) data frames (inner, outer, left, right)Convert data.frame columns from factors to charactersR - list to data frameDrop data frame columns by nameRemove rows with all or some NAs (missing values) in data.frameCreate an empty data.frameHow to get a value from a cell of a dataframe?How to append rows to an R data frame

Landlord wants to switch my lease to a "Land contract" to "get back at the city"

Domain expired, GoDaddy holds it and is asking more money

Re-submission of rejected manuscript without informing co-authors

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Why do we use polarized capacitors?

LWC and complex parameters

New order #4: World

What is the command to reset a PC without deleting any files

aging parents with no investments

Doomsday-clock for my fantasy planet

Why is the design of haulage companies so “special”?

Does the average primeness of natural numbers tend to zero?

Could Giant Ground Sloths have been a good pack animal for the ancient Mayans?

Can I find out the caloric content of bread by dehydrating it?

Is ipsum/ipsa/ipse a third person pronoun, or can it serve other functions?

Can the Produce Flame cantrip be used to grapple, or as an unarmed strike, in the right circumstances?

Crop image to path created in TikZ?

Does bootstrapped regression allow for inference?

If a centaur druid Wild Shapes into a Giant Elk, do their Charge features stack?

What does "enim et" mean?

Is "plugging out" electronic devices an American expression?

extract characters between two commas?

Is there a way to make member function NOT callable from constructor?

Is it legal to have the "// (c) 2019 John Smith" header in all files when there are hundreds of contributors?



Replace all particular values in a data frame


Using regex to set a specific digit to NA?Remove Characters in Rparsing quotes out of “NA” stringsReplace specific characters in a variable in data frame in RRegression Analysis in RReplace character in the whole dataframeReplace values in an R data frame in Python using rpy2Join values when creating boxplotHow to replace a character by a newline in Vim?How to replace all occurrences of a string in JavaScriptHow to join (merge) data frames (inner, outer, left, right)Convert data.frame columns from factors to charactersR - list to data frameDrop data frame columns by nameRemove rows with all or some NAs (missing values) in data.frameCreate an empty data.frameHow to get a value from a cell of a dataframe?How to append rows to an R data frame






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








66















Having a data frame, how do I go about replacing all particular values along all rows and columns. Say for example I want to replace all empty records with NA's (without typing the positions):



df <- data.frame(list(A=c("", "xyz", "jkl"), B=c(12, "", 100)))

A B
1 12
2 xyz
3 jkl 100


Expected result:



 A B
1 NA 12
2 xyz NA
3 jkl 100









share|improve this question






























    66















    Having a data frame, how do I go about replacing all particular values along all rows and columns. Say for example I want to replace all empty records with NA's (without typing the positions):



    df <- data.frame(list(A=c("", "xyz", "jkl"), B=c(12, "", 100)))

    A B
    1 12
    2 xyz
    3 jkl 100


    Expected result:



     A B
    1 NA 12
    2 xyz NA
    3 jkl 100









    share|improve this question


























      66












      66








      66


      21






      Having a data frame, how do I go about replacing all particular values along all rows and columns. Say for example I want to replace all empty records with NA's (without typing the positions):



      df <- data.frame(list(A=c("", "xyz", "jkl"), B=c(12, "", 100)))

      A B
      1 12
      2 xyz
      3 jkl 100


      Expected result:



       A B
      1 NA 12
      2 xyz NA
      3 jkl 100









      share|improve this question
















      Having a data frame, how do I go about replacing all particular values along all rows and columns. Say for example I want to replace all empty records with NA's (without typing the positions):



      df <- data.frame(list(A=c("", "xyz", "jkl"), B=c(12, "", 100)))

      A B
      1 12
      2 xyz
      3 jkl 100


      Expected result:



       A B
      1 NA 12
      2 xyz NA
      3 jkl 100






      r dataframe replace






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 5 '17 at 3:12









      Ronak Shah

      45.5k104267




      45.5k104267










      asked Oct 21 '13 at 19:41









      zxzakzxzak

      3,91521922




      3,91521922






















          5 Answers
          5






          active

          oldest

          votes


















          97














          Like this:



          > df[df==""]<-NA
          > df
          A B
          1 <NA> 12
          2 xyz <NA>
          3 jkl 100





          share|improve this answer


















          • 11





            is there a way to do this efficiently for more than 1 value!?

            – PikkuKatja
            Mar 11 '15 at 10:23






          • 14





            This doesn't work for factors, df[df=="xyz"]<-"abc" will error with "invalid factor level." Is there a more general solution?

            – glallen
            Sep 2 '15 at 4:22











          • not working for me. I tried this: dfSmallDiscreteCustomSalary[dfSmallDiscreteCustomSalary$salary=="<=50K"] <- "49K". Still for unique(dfSmallDiscreteCustomSalary$salary) i get: [1] >50K <=50K

            – Codious-JR
            Nov 5 '15 at 12:24







          • 3





            glallen ... if you're trying to modify a factor column with a new value that already a factor, there are probably more clever ways that what I'm about to suggest, but you could df$factorcolumn <- as.character(df$factorcolumn), then make your modification, and finish off by turning it back into a factor again... df$factorcolumn <- as.factor(df$factorcolumn); it'll be complete with your new level and desired value.

            – Joshua Eric Turcotte
            Oct 25 '17 at 22:54






          • 1





            Scala equivalent for this please..

            – sriram
            Oct 27 '17 at 19:51


















          22














          Since PikkuKatja and glallen asked for a more general solution and I cannot comment yet, I'll write an answer. You can combine statements as in:



          > df[df=="" | df==12] <- NA
          > df
          A B
          1 <NA> <NA>
          2 xyz <NA>
          3 jkl 100


          For factors, zxzak's code already yields factors:



          > df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)))
          > str(df)
          'data.frame': 3 obs. of 2 variables:
          $ A: Factor w/ 3 levels "","jkl","xyz": 1 3 2
          $ B: Factor w/ 3 levels "","100","12": 3 1 2


          If in trouble, I'd suggest to temporarily drop the factors.



          df[] <- lapply(df, as.character)





          share|improve this answer






























            3














            We can use data.table to get it quickly.
            First create df without factors,



            df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)), stringsAsFactors=F)


            Now you can use



            setDT(df)
            for (jj in 1:ncol(df)) set(df, i = which(df[[jj]]==""), j = jj, v = NA)


            and you can convert it back to a data.frame



            setDF(df)


            If you only want to use data.frame and keep factors it's more difficult, you need to work with



            levels(df$value)[levels(df$value)==""] <- NA


            where value is the name of every column. You need to insert it in a loop.






            share|improve this answer


















            • 2





              Why would you use an external library for this use case? Why a loop if this can be solved with one line? How does your answer add value beyond the answers already present? I don't intend to be harsh, I think I am missing something, hence the questions.

              – sedot
              Jun 21 '17 at 23:34






            • 2





              It's much faster for large datasets. It adds an alternative so that the user can choose the best for him.

              – skan
              Jun 22 '17 at 9:12



















            0














            If you want to replace multiple values in a data frame, looping through all columns might help.



            Say you want to replace "" and 100:



            na_codes <- c(100, "")
            for (i in seq_along(df))
            df[[i]][df[[i]] %in% na_codes] <- NA






            share|improve this answer






























              0














              Here are a couple dplyr options:



              library(dplyr)

              # all columns:
              df %>%
              mutate_all(~na_if(., ''))

              # specific column types:
              df %>%
              mutate_if(is.factor, ~na_if(., ''))

              # specific columns:
              df %>%
              mutate_at(vars(A, B), ~na_if(., ''))

              # or:
              df %>%
              mutate(A = replace(A, A == '', NA))

              # replace can be used if you want something other than NA:
              df %>%
              mutate(A = as.character(A)) %>%
              mutate(A = replace(A, A == '', 'used to be empty'))





              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%2f19503266%2freplace-all-particular-values-in-a-data-frame%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                97














                Like this:



                > df[df==""]<-NA
                > df
                A B
                1 <NA> 12
                2 xyz <NA>
                3 jkl 100





                share|improve this answer


















                • 11





                  is there a way to do this efficiently for more than 1 value!?

                  – PikkuKatja
                  Mar 11 '15 at 10:23






                • 14





                  This doesn't work for factors, df[df=="xyz"]<-"abc" will error with "invalid factor level." Is there a more general solution?

                  – glallen
                  Sep 2 '15 at 4:22











                • not working for me. I tried this: dfSmallDiscreteCustomSalary[dfSmallDiscreteCustomSalary$salary=="<=50K"] <- "49K". Still for unique(dfSmallDiscreteCustomSalary$salary) i get: [1] >50K <=50K

                  – Codious-JR
                  Nov 5 '15 at 12:24







                • 3





                  glallen ... if you're trying to modify a factor column with a new value that already a factor, there are probably more clever ways that what I'm about to suggest, but you could df$factorcolumn <- as.character(df$factorcolumn), then make your modification, and finish off by turning it back into a factor again... df$factorcolumn <- as.factor(df$factorcolumn); it'll be complete with your new level and desired value.

                  – Joshua Eric Turcotte
                  Oct 25 '17 at 22:54






                • 1





                  Scala equivalent for this please..

                  – sriram
                  Oct 27 '17 at 19:51















                97














                Like this:



                > df[df==""]<-NA
                > df
                A B
                1 <NA> 12
                2 xyz <NA>
                3 jkl 100





                share|improve this answer


















                • 11





                  is there a way to do this efficiently for more than 1 value!?

                  – PikkuKatja
                  Mar 11 '15 at 10:23






                • 14





                  This doesn't work for factors, df[df=="xyz"]<-"abc" will error with "invalid factor level." Is there a more general solution?

                  – glallen
                  Sep 2 '15 at 4:22











                • not working for me. I tried this: dfSmallDiscreteCustomSalary[dfSmallDiscreteCustomSalary$salary=="<=50K"] <- "49K". Still for unique(dfSmallDiscreteCustomSalary$salary) i get: [1] >50K <=50K

                  – Codious-JR
                  Nov 5 '15 at 12:24







                • 3





                  glallen ... if you're trying to modify a factor column with a new value that already a factor, there are probably more clever ways that what I'm about to suggest, but you could df$factorcolumn <- as.character(df$factorcolumn), then make your modification, and finish off by turning it back into a factor again... df$factorcolumn <- as.factor(df$factorcolumn); it'll be complete with your new level and desired value.

                  – Joshua Eric Turcotte
                  Oct 25 '17 at 22:54






                • 1





                  Scala equivalent for this please..

                  – sriram
                  Oct 27 '17 at 19:51













                97












                97








                97







                Like this:



                > df[df==""]<-NA
                > df
                A B
                1 <NA> 12
                2 xyz <NA>
                3 jkl 100





                share|improve this answer













                Like this:



                > df[df==""]<-NA
                > df
                A B
                1 <NA> 12
                2 xyz <NA>
                3 jkl 100






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 21 '13 at 19:44









                mripmrip

                10.9k22446




                10.9k22446







                • 11





                  is there a way to do this efficiently for more than 1 value!?

                  – PikkuKatja
                  Mar 11 '15 at 10:23






                • 14





                  This doesn't work for factors, df[df=="xyz"]<-"abc" will error with "invalid factor level." Is there a more general solution?

                  – glallen
                  Sep 2 '15 at 4:22











                • not working for me. I tried this: dfSmallDiscreteCustomSalary[dfSmallDiscreteCustomSalary$salary=="<=50K"] <- "49K". Still for unique(dfSmallDiscreteCustomSalary$salary) i get: [1] >50K <=50K

                  – Codious-JR
                  Nov 5 '15 at 12:24







                • 3





                  glallen ... if you're trying to modify a factor column with a new value that already a factor, there are probably more clever ways that what I'm about to suggest, but you could df$factorcolumn <- as.character(df$factorcolumn), then make your modification, and finish off by turning it back into a factor again... df$factorcolumn <- as.factor(df$factorcolumn); it'll be complete with your new level and desired value.

                  – Joshua Eric Turcotte
                  Oct 25 '17 at 22:54






                • 1





                  Scala equivalent for this please..

                  – sriram
                  Oct 27 '17 at 19:51












                • 11





                  is there a way to do this efficiently for more than 1 value!?

                  – PikkuKatja
                  Mar 11 '15 at 10:23






                • 14





                  This doesn't work for factors, df[df=="xyz"]<-"abc" will error with "invalid factor level." Is there a more general solution?

                  – glallen
                  Sep 2 '15 at 4:22











                • not working for me. I tried this: dfSmallDiscreteCustomSalary[dfSmallDiscreteCustomSalary$salary=="<=50K"] <- "49K". Still for unique(dfSmallDiscreteCustomSalary$salary) i get: [1] >50K <=50K

                  – Codious-JR
                  Nov 5 '15 at 12:24







                • 3





                  glallen ... if you're trying to modify a factor column with a new value that already a factor, there are probably more clever ways that what I'm about to suggest, but you could df$factorcolumn <- as.character(df$factorcolumn), then make your modification, and finish off by turning it back into a factor again... df$factorcolumn <- as.factor(df$factorcolumn); it'll be complete with your new level and desired value.

                  – Joshua Eric Turcotte
                  Oct 25 '17 at 22:54






                • 1





                  Scala equivalent for this please..

                  – sriram
                  Oct 27 '17 at 19:51







                11




                11





                is there a way to do this efficiently for more than 1 value!?

                – PikkuKatja
                Mar 11 '15 at 10:23





                is there a way to do this efficiently for more than 1 value!?

                – PikkuKatja
                Mar 11 '15 at 10:23




                14




                14





                This doesn't work for factors, df[df=="xyz"]<-"abc" will error with "invalid factor level." Is there a more general solution?

                – glallen
                Sep 2 '15 at 4:22





                This doesn't work for factors, df[df=="xyz"]<-"abc" will error with "invalid factor level." Is there a more general solution?

                – glallen
                Sep 2 '15 at 4:22













                not working for me. I tried this: dfSmallDiscreteCustomSalary[dfSmallDiscreteCustomSalary$salary=="<=50K"] <- "49K". Still for unique(dfSmallDiscreteCustomSalary$salary) i get: [1] >50K <=50K

                – Codious-JR
                Nov 5 '15 at 12:24






                not working for me. I tried this: dfSmallDiscreteCustomSalary[dfSmallDiscreteCustomSalary$salary=="<=50K"] <- "49K". Still for unique(dfSmallDiscreteCustomSalary$salary) i get: [1] >50K <=50K

                – Codious-JR
                Nov 5 '15 at 12:24





                3




                3





                glallen ... if you're trying to modify a factor column with a new value that already a factor, there are probably more clever ways that what I'm about to suggest, but you could df$factorcolumn <- as.character(df$factorcolumn), then make your modification, and finish off by turning it back into a factor again... df$factorcolumn <- as.factor(df$factorcolumn); it'll be complete with your new level and desired value.

                – Joshua Eric Turcotte
                Oct 25 '17 at 22:54





                glallen ... if you're trying to modify a factor column with a new value that already a factor, there are probably more clever ways that what I'm about to suggest, but you could df$factorcolumn <- as.character(df$factorcolumn), then make your modification, and finish off by turning it back into a factor again... df$factorcolumn <- as.factor(df$factorcolumn); it'll be complete with your new level and desired value.

                – Joshua Eric Turcotte
                Oct 25 '17 at 22:54




                1




                1





                Scala equivalent for this please..

                – sriram
                Oct 27 '17 at 19:51





                Scala equivalent for this please..

                – sriram
                Oct 27 '17 at 19:51













                22














                Since PikkuKatja and glallen asked for a more general solution and I cannot comment yet, I'll write an answer. You can combine statements as in:



                > df[df=="" | df==12] <- NA
                > df
                A B
                1 <NA> <NA>
                2 xyz <NA>
                3 jkl 100


                For factors, zxzak's code already yields factors:



                > df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)))
                > str(df)
                'data.frame': 3 obs. of 2 variables:
                $ A: Factor w/ 3 levels "","jkl","xyz": 1 3 2
                $ B: Factor w/ 3 levels "","100","12": 3 1 2


                If in trouble, I'd suggest to temporarily drop the factors.



                df[] <- lapply(df, as.character)





                share|improve this answer



























                  22














                  Since PikkuKatja and glallen asked for a more general solution and I cannot comment yet, I'll write an answer. You can combine statements as in:



                  > df[df=="" | df==12] <- NA
                  > df
                  A B
                  1 <NA> <NA>
                  2 xyz <NA>
                  3 jkl 100


                  For factors, zxzak's code already yields factors:



                  > df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)))
                  > str(df)
                  'data.frame': 3 obs. of 2 variables:
                  $ A: Factor w/ 3 levels "","jkl","xyz": 1 3 2
                  $ B: Factor w/ 3 levels "","100","12": 3 1 2


                  If in trouble, I'd suggest to temporarily drop the factors.



                  df[] <- lapply(df, as.character)





                  share|improve this answer

























                    22












                    22








                    22







                    Since PikkuKatja and glallen asked for a more general solution and I cannot comment yet, I'll write an answer. You can combine statements as in:



                    > df[df=="" | df==12] <- NA
                    > df
                    A B
                    1 <NA> <NA>
                    2 xyz <NA>
                    3 jkl 100


                    For factors, zxzak's code already yields factors:



                    > df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)))
                    > str(df)
                    'data.frame': 3 obs. of 2 variables:
                    $ A: Factor w/ 3 levels "","jkl","xyz": 1 3 2
                    $ B: Factor w/ 3 levels "","100","12": 3 1 2


                    If in trouble, I'd suggest to temporarily drop the factors.



                    df[] <- lapply(df, as.character)





                    share|improve this answer













                    Since PikkuKatja and glallen asked for a more general solution and I cannot comment yet, I'll write an answer. You can combine statements as in:



                    > df[df=="" | df==12] <- NA
                    > df
                    A B
                    1 <NA> <NA>
                    2 xyz <NA>
                    3 jkl 100


                    For factors, zxzak's code already yields factors:



                    > df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)))
                    > str(df)
                    'data.frame': 3 obs. of 2 variables:
                    $ A: Factor w/ 3 levels "","jkl","xyz": 1 3 2
                    $ B: Factor w/ 3 levels "","100","12": 3 1 2


                    If in trouble, I'd suggest to temporarily drop the factors.



                    df[] <- lapply(df, as.character)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 8 '15 at 1:12









                    sedotsedot

                    319312




                    319312





















                        3














                        We can use data.table to get it quickly.
                        First create df without factors,



                        df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)), stringsAsFactors=F)


                        Now you can use



                        setDT(df)
                        for (jj in 1:ncol(df)) set(df, i = which(df[[jj]]==""), j = jj, v = NA)


                        and you can convert it back to a data.frame



                        setDF(df)


                        If you only want to use data.frame and keep factors it's more difficult, you need to work with



                        levels(df$value)[levels(df$value)==""] <- NA


                        where value is the name of every column. You need to insert it in a loop.






                        share|improve this answer


















                        • 2





                          Why would you use an external library for this use case? Why a loop if this can be solved with one line? How does your answer add value beyond the answers already present? I don't intend to be harsh, I think I am missing something, hence the questions.

                          – sedot
                          Jun 21 '17 at 23:34






                        • 2





                          It's much faster for large datasets. It adds an alternative so that the user can choose the best for him.

                          – skan
                          Jun 22 '17 at 9:12
















                        3














                        We can use data.table to get it quickly.
                        First create df without factors,



                        df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)), stringsAsFactors=F)


                        Now you can use



                        setDT(df)
                        for (jj in 1:ncol(df)) set(df, i = which(df[[jj]]==""), j = jj, v = NA)


                        and you can convert it back to a data.frame



                        setDF(df)


                        If you only want to use data.frame and keep factors it's more difficult, you need to work with



                        levels(df$value)[levels(df$value)==""] <- NA


                        where value is the name of every column. You need to insert it in a loop.






                        share|improve this answer


















                        • 2





                          Why would you use an external library for this use case? Why a loop if this can be solved with one line? How does your answer add value beyond the answers already present? I don't intend to be harsh, I think I am missing something, hence the questions.

                          – sedot
                          Jun 21 '17 at 23:34






                        • 2





                          It's much faster for large datasets. It adds an alternative so that the user can choose the best for him.

                          – skan
                          Jun 22 '17 at 9:12














                        3












                        3








                        3







                        We can use data.table to get it quickly.
                        First create df without factors,



                        df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)), stringsAsFactors=F)


                        Now you can use



                        setDT(df)
                        for (jj in 1:ncol(df)) set(df, i = which(df[[jj]]==""), j = jj, v = NA)


                        and you can convert it back to a data.frame



                        setDF(df)


                        If you only want to use data.frame and keep factors it's more difficult, you need to work with



                        levels(df$value)[levels(df$value)==""] <- NA


                        where value is the name of every column. You need to insert it in a loop.






                        share|improve this answer













                        We can use data.table to get it quickly.
                        First create df without factors,



                        df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)), stringsAsFactors=F)


                        Now you can use



                        setDT(df)
                        for (jj in 1:ncol(df)) set(df, i = which(df[[jj]]==""), j = jj, v = NA)


                        and you can convert it back to a data.frame



                        setDF(df)


                        If you only want to use data.frame and keep factors it's more difficult, you need to work with



                        levels(df$value)[levels(df$value)==""] <- NA


                        where value is the name of every column. You need to insert it in a loop.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 28 '16 at 19:28









                        skanskan

                        2,88183461




                        2,88183461







                        • 2





                          Why would you use an external library for this use case? Why a loop if this can be solved with one line? How does your answer add value beyond the answers already present? I don't intend to be harsh, I think I am missing something, hence the questions.

                          – sedot
                          Jun 21 '17 at 23:34






                        • 2





                          It's much faster for large datasets. It adds an alternative so that the user can choose the best for him.

                          – skan
                          Jun 22 '17 at 9:12













                        • 2





                          Why would you use an external library for this use case? Why a loop if this can be solved with one line? How does your answer add value beyond the answers already present? I don't intend to be harsh, I think I am missing something, hence the questions.

                          – sedot
                          Jun 21 '17 at 23:34






                        • 2





                          It's much faster for large datasets. It adds an alternative so that the user can choose the best for him.

                          – skan
                          Jun 22 '17 at 9:12








                        2




                        2





                        Why would you use an external library for this use case? Why a loop if this can be solved with one line? How does your answer add value beyond the answers already present? I don't intend to be harsh, I think I am missing something, hence the questions.

                        – sedot
                        Jun 21 '17 at 23:34





                        Why would you use an external library for this use case? Why a loop if this can be solved with one line? How does your answer add value beyond the answers already present? I don't intend to be harsh, I think I am missing something, hence the questions.

                        – sedot
                        Jun 21 '17 at 23:34




                        2




                        2





                        It's much faster for large datasets. It adds an alternative so that the user can choose the best for him.

                        – skan
                        Jun 22 '17 at 9:12






                        It's much faster for large datasets. It adds an alternative so that the user can choose the best for him.

                        – skan
                        Jun 22 '17 at 9:12












                        0














                        If you want to replace multiple values in a data frame, looping through all columns might help.



                        Say you want to replace "" and 100:



                        na_codes <- c(100, "")
                        for (i in seq_along(df))
                        df[[i]][df[[i]] %in% na_codes] <- NA






                        share|improve this answer



























                          0














                          If you want to replace multiple values in a data frame, looping through all columns might help.



                          Say you want to replace "" and 100:



                          na_codes <- c(100, "")
                          for (i in seq_along(df))
                          df[[i]][df[[i]] %in% na_codes] <- NA






                          share|improve this answer

























                            0












                            0








                            0







                            If you want to replace multiple values in a data frame, looping through all columns might help.



                            Say you want to replace "" and 100:



                            na_codes <- c(100, "")
                            for (i in seq_along(df))
                            df[[i]][df[[i]] %in% na_codes] <- NA






                            share|improve this answer













                            If you want to replace multiple values in a data frame, looping through all columns might help.



                            Say you want to replace "" and 100:



                            na_codes <- c(100, "")
                            for (i in seq_along(df))
                            df[[i]][df[[i]] %in% na_codes] <- NA







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 7 '17 at 2:11









                            Olivier MaOlivier Ma

                            683614




                            683614





















                                0














                                Here are a couple dplyr options:



                                library(dplyr)

                                # all columns:
                                df %>%
                                mutate_all(~na_if(., ''))

                                # specific column types:
                                df %>%
                                mutate_if(is.factor, ~na_if(., ''))

                                # specific columns:
                                df %>%
                                mutate_at(vars(A, B), ~na_if(., ''))

                                # or:
                                df %>%
                                mutate(A = replace(A, A == '', NA))

                                # replace can be used if you want something other than NA:
                                df %>%
                                mutate(A = as.character(A)) %>%
                                mutate(A = replace(A, A == '', 'used to be empty'))





                                share|improve this answer





























                                  0














                                  Here are a couple dplyr options:



                                  library(dplyr)

                                  # all columns:
                                  df %>%
                                  mutate_all(~na_if(., ''))

                                  # specific column types:
                                  df %>%
                                  mutate_if(is.factor, ~na_if(., ''))

                                  # specific columns:
                                  df %>%
                                  mutate_at(vars(A, B), ~na_if(., ''))

                                  # or:
                                  df %>%
                                  mutate(A = replace(A, A == '', NA))

                                  # replace can be used if you want something other than NA:
                                  df %>%
                                  mutate(A = as.character(A)) %>%
                                  mutate(A = replace(A, A == '', 'used to be empty'))





                                  share|improve this answer



























                                    0












                                    0








                                    0







                                    Here are a couple dplyr options:



                                    library(dplyr)

                                    # all columns:
                                    df %>%
                                    mutate_all(~na_if(., ''))

                                    # specific column types:
                                    df %>%
                                    mutate_if(is.factor, ~na_if(., ''))

                                    # specific columns:
                                    df %>%
                                    mutate_at(vars(A, B), ~na_if(., ''))

                                    # or:
                                    df %>%
                                    mutate(A = replace(A, A == '', NA))

                                    # replace can be used if you want something other than NA:
                                    df %>%
                                    mutate(A = as.character(A)) %>%
                                    mutate(A = replace(A, A == '', 'used to be empty'))





                                    share|improve this answer















                                    Here are a couple dplyr options:



                                    library(dplyr)

                                    # all columns:
                                    df %>%
                                    mutate_all(~na_if(., ''))

                                    # specific column types:
                                    df %>%
                                    mutate_if(is.factor, ~na_if(., ''))

                                    # specific columns:
                                    df %>%
                                    mutate_at(vars(A, B), ~na_if(., ''))

                                    # or:
                                    df %>%
                                    mutate(A = replace(A, A == '', NA))

                                    # replace can be used if you want something other than NA:
                                    df %>%
                                    mutate(A = as.character(A)) %>%
                                    mutate(A = replace(A, A == '', 'used to be empty'))






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Mar 22 at 10:44

























                                    answered Mar 22 at 1:48









                                    sbhasbha

                                    2,62822226




                                    2,62822226



























                                        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%2f19503266%2freplace-all-particular-values-in-a-data-frame%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

                                        Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                        Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                        Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript