Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace missing values (NA) with blank (empty string)

Tags:

r

na

I have a dataframe with an NA row:

 df = data.frame(c("classA", NA, "classB"), t(data.frame(rep("A", 5), rep(NA, 5), rep("B", 5))))  rownames(df) <- c(1,2,3)  colnames(df) <- c("class", paste("Year", 1:5, sep = ""))   > df    class Year1 Year2 Year3 Year4 Year5 1 classA     A     A     A     A     A 2   <NA>  <NA>  <NA>  <NA>  <NA>  <NA> 3 classB     B     B     B     B     B 

I introduced the empty row (NA row) on purpose because I wanted to have some space between classA row and classB row.

Now, I would like to substitute the <NA> by blank, so that the second row looks like an empty row.

I tried:

 df[is.na(df)] <- "" 

and

 df[df == "NA"] <- "" 

but it didn't work..

Any ideas? Thanks!

like image 860
Mayou Avatar asked Oct 25 '13 14:10

Mayou


People also ask

How do I replace missing values with NA in R?

To replace missing values in R with the minimum, you can use the tidyverse package. Firstly, you use the mutate() function to specify the column in which you want to replace the missing values. Secondly, you call the replace() function to identify the NA's and to substitute them with the column lowest value.

How do I replace a string with a blank in R?

Using R replace() function to update Empty String with NA R has a built-in function called replace() that replaces values in a vector with another value, for example, blank space with NAs.

How do you make all NA 0?

Replace NA with 0 in R Data Frame To replace NA with 0 in an R data frame, use is.na() function and then select all those values with NA and assign them to 0.


2 Answers

Another alternative:

df <- sapply(df, as.character) # since your values are `factor` df[is.na(df)] <- 0 

If you want blanks instead of zeroes

> df <- sapply(df, as.character) > df[is.na(df)] <- " " > df      class    Year1 Year2 Year3 Year4 Year5 [1,] "classA" "A"   "A"   "A"   "A"   "A"   [2,] " "      " "   " "   " "   " "   " "   [3,] "classB" "B"   "B"   "B"   "B"   "B"   

If you want a data.frame, then just use as.data.drame

> as.data.frame(df)    class Year1 Year2 Year3 Year4 Year5 1 classA     A     A     A     A     A 2                                      3 classB     B     B     B     B     B 
like image 96
Jilber Urbina Avatar answered Sep 20 '22 07:09

Jilber Urbina


This answer is more of an extended comment.

What you're trying to do isn't what I would consider good practice. R is not, say, Excel, so doing something like this just to create visual separation in your data is just going to give you a headache later on down the line.

If you really only cared about the visual output, I can offer two suggestions:

  1. Use the na.print argument to print when you want to view the data with that visual separation.

    print(df, na.print = "") #    class Year1 Year2 Year3 Year4 Year5 # 1 classA     A     A     A     A     A # 2                                      # 3 classB     B     B     B     B     B 
  2. Realize that even the above is not the best suggestion. Get both visual and content separation by converting your data.frame to a list:

    split(df, df$class) # $classA #    class Year1 Year2 Year3 Year4 Year5 # 1 classA     A     A     A     A     A #  # $classB #    class Year1 Year2 Year3 Year4 Year5 # 3 classB     B     B     B     B     B 
like image 34
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 22 '22 07:09

A5C1D2H2I1M1N2O1R2T1