Michael S Taylor
Michael S Taylor

Reputation: 491

Replace NA with zero when separating with tidyr?

I have a column of decimals in a tibble that I want to separate into the integer and non-integer components. If the part after the decimal point is exactly zero, then the separate() function turns it into NA.

library("tidyverse") # or
# library("tibble")
# library("tidyr")

x <- c(1992.345, 1993.000, 1993.544)
y <- c(31.2, 32.3, 33.4)
dat <- tibble(x, y)

dat %>% 
  separate(x,
           into = c("x1", "x2"),
           convert = TRUE,
           fill = "right")

#> # A tibble: 3 x 3
#>      x1    x2     y
#>   <int> <int> <dbl>
#> 1  1992   345  31.2
#> 2  1993    NA  32.3
#> 3  1993   544  33.4

I would like to tell separate() to retain the zero instead of replacing it with NA? I know that I can mutate() and replace_na(), e.g.

dat %>% 
  separate(x,
           into = c("x1", "x2"),
           convert = TRUE,
           fill = "right") %>% 
  mutate(x2 = replace_na(x2, 0))

#> # A tibble: 3 x 3
#>      x1    x2     y
#>   <int> <dbl> <dbl>
#> 1  1992   345  31.2
#> 2  1993     0  32.3
#> 3  1993   544  33.4

Question: Can I skip the mutate() step and replace NA with zero in the separate() step? Or, is this about as concise as it can get? I am open to other solutions.

Created on 2018-11-07 by the reprex package (v0.2.1)

Upvotes: 2

Views: 556

Answers (1)

www
www

Reputation: 39154

We can format the x column before using the separate function.

dat %>% 
  mutate(x = format(x, nsmall = 3)) %>%
  separate(x,
           into = c("x1", "x2"),
           convert = TRUE,
           fill = "right")
# # A tibble: 3 x 3
#      x1    x2     y
#   <int> <int> <dbl>
# 1  1992   345  31.2
# 2  1993     0  32.3
# 3  1993   544  33.4

Upvotes: 4

Related Questions