NA replacing with blanks
Asked Answered
I

3

5

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 ("")

Instauration answered 28/1, 2020 at 19:13 Comment(4)
Some of the columns are numeric and by doing the replacement to ' ', it converts to characterIronic
The quotation marks are so you can see that you've put a space there, which doesn't seem like what you actually wantCamphorate
Does this answer your question? How do I replace NA values with zeros in an R dataframe?Camphorate
@Camphorate not exactly the same, similar yes, same no!Monetmoneta
B
1

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)
Boon answered 28/1, 2020 at 19:33 Comment(0)
I
3

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"
Ironic answered 28/1, 2020 at 19:16 Comment(7)
Thanks akrun but I am still get the " " instead of blanks.Instauration
"" is blank. What are you expecting?Boon
@Instauration Not able to reproduce what you mentioned. If you need to change the format of print, use %>% as.data.frame at the endIronic
I think the confusion is that print displays "" in data.frame as blank and tibble displays "" as "". Actually data.frame does't show quotation marks in stringsBoon
Mosquite, I don't want quotation marks, I just want a blank field. I am printing a kable and I don't want quotation marks showing.Instauration
Does it show the quotation marks when you print as a kable?Boon
Both of you are correct. My dataset is a tibble and needed to convert to data.frame. What a great lesson. Is there a way to give credit to both?Instauration
M
3

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
Monetmoneta answered 28/1, 2020 at 19:27 Comment(0)
B
1

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)
Boon answered 28/1, 2020 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.