How to replace empty rows in dataframe with NA (missing value), and not with row NA

I have a titanic xlsx file that has many empty or empty cells, and I saved the file as csv and all the spaces were saved as is.

When I import a csv file, I see a lot of empty lines / spaces in the data set, one of these columns is a boat

I could just go and use readxl package functions like read_xls or read_xlsx that would replace empty NA lines

But I would like to know if there is a way if I can replace the empty lines after loading in R in the dataframe.

I tried this way, but it causes an error that I don't understand exactly. I can specify NA in "NA" in the code below, then it will replace NA, but it will be a string (NA) not missing the NA value, both will be different.

titanic %>% mutate(boat = if_else(boat=="", NA ,boat))

Error in mutate_impl(.data, dots) : 
Evaluation error: `false` must be type logical, not character.
+4
source share
2 answers

By indicating only NA, according to ?NA- "NA is a logical constant of length 1 that contains the missing value."

class can check

class(NA)
#[1] "logical"
class(NA_character_) 
#[1] "character"

and both are identified by standard features such as is.na

is.na(NA)
#[1] TRUE
is.na(NA_character_)
#[1] TRUE

if_else , NA, , NA_real_, NA_integer_, NA_character_ "", . , "" - character, NA_character_

titanic %>% 
       mutate(boat = if_else(boat=="", NA_character_ ,boat))
+3

NA naniar - http://naniar.njtierney.com/


df <- data.frame(boat = c(1, 2, "", 3), category = c("a", "b", "c", "d"))


df
#>   boat category
#> 1    1        a
#> 2    2        b
#> 3             c
#> 4    3        d
library(naniar)

df %>% replace_with_na(replace = list(boat = ""))
#>   boat category
#> 1    1        a
#> 2    2        b
#> 3 <NA>        c
#> 4    3        d

# You can also specify how to do this for a specific, using the development
# version - devtools::install_github('njtierney/naniar')
df %>% replace_with_na_at(.vars = "boat", ~.x == "")
#>   boat category
#> 1    2        a
#> 2    3        b
#> 3   NA        c
#> 4    4        d

, !

+1

Source: https://habr.com/ru/post/1692339/


All Articles