Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NA replacing with blanks

Tags:

replace

r

I have used the same method for replacing NA's with blanks or other characters but for some reason this one is not working. I want to replace the NA's on my dataframe to blanks (columns year and Annual). What am I doing wrong?

shad.92 <- structure(list(year = c(1992, NA, NA, NA, NA), type = c("all age abundance index", 
"adjusted number of fish older than age 0 measured", "adjusted total number of fish measured", 
"percent YOY", "YOY abundance index"), September = c(755, 0, 
565, 100, 755), October = c(530, 0, 434, 100, 530), November = c(463, 
0, 338, 100, 463), December = c(266, 1, 136, 99.3, 264), Annual = c(2014, 
NA, NA, NA, 2012)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

I tried this with no luck:

shad.92 <- shad.92[is.na(shad.92)] <- ""

I then tried one column at the time starting with year:

shad.92$year <- as.character(shad.92$year)
    shad.92$year[is.na(shad.92$year)] <- " "
    shad.92 

But I get quotation marks instead of blanks ("")

like image 485
Salvador Avatar asked Jan 28 '20 19:01

Salvador


People also ask

How do I replace Na with blank?

How to replace NA (missing values) with blank space or an empty string in an R dataframe? You can replace NA values with blank space on columns of R dataframe (data. frame) by using is.na() , replace() methods.


3 Answers

We can use mutate_at with replace_na

library(dplyr)
library(tidyr)
shad.92 %>% 
       mutate_at(vars(year, Annual), replace_na, '')
# A tibble: 5 x 7
#  year   type                                              September October November December Annual
#  <chr>  <chr>                                                 <dbl>   <dbl>    <dbl>    <dbl> <chr> 
#1 "1992" all age abundance index                                 755     530      463    266   "2014"
#2 ""     adjusted number of fish older than age 0 measured         0       0        0      1   ""    
#3 ""     adjusted total number of fish measured                  565     434      338    136   ""    
#4 ""     percent YOY                                             100     100      100     99.3 ""    
#5 ""     YOY abundance index                                     755     530      463    264   "2012"
like image 130
akrun Avatar answered Oct 18 '22 23:10

akrun


A base solution using replace:

as.data.frame(replace(shad.92, is.na(shad.92), ""))

#>   year                                              type September October
#> 1 1992                           all age abundance index       755     530
#> 2      adjusted number of fish older than age 0 measured         0       0
#> 3                 adjusted total number of fish measured       565     434
#> 4                                            percent YOY       100     100
#> 5                                    YOY abundance index       755     530
#>   November December Annual
#> 1      463      266   2014
#> 2        0        1       
#> 3      338      136       
#> 4      100     99.3       
#> 5      463      264   2012
like image 40
M-- Avatar answered Oct 18 '22 22:10

M--


When you have "" in a tibble it displays the elements of that column with the quotation marks. However "" is a blank. When you use base R's data.frame the quotations are not printed. Try the below after you run your code.

as.data.frame(shad.92)

But if you want to print this as a kable, the kable should not print the "" itself (at least not to the terminal; I didn't try printing to HTML).

library(knitr)
kable(shad.92)
like image 1
Mosquite Avatar answered Oct 18 '22 23:10

Mosquite