How to replace blanks with NA in dataframe

I want to replace blanks with NA but only in the first column of dataframe AND alslo reference it by column number rather than title

is that possible with dplyr ?

cheers

I am not totally sure what you mean by "blanks", so I assumed you meant an empty string. So here is how I would do it:

library(dplyr)

data <- tibble(
  x = c('A', '', 'C'),
  y = 1:3
)
data
#> # A tibble: 3 × 2
#>   x         y
#>   <chr> <int>
#> 1 "A"       1
#> 2 ""        2
#> 3 "C"       3

data %>% 
  mutate(
    across(1, na_if, y = '')
  )
#> # A tibble: 3 × 2
#>   x         y
#>   <chr> <int>
#> 1 A         1
#> 2 <NA>      2
#> 3 C         3

Thanks a lot..

So how would I now user is.na but based on column number rather than name ?

If you want to keep the row that now has NA in the first column,

data <- tibble(
   x = c('A', '', 'C'),
   y = 1:3
 )
data |>  
   mutate(
     across(1, na_if, y = '')
   ) |> 
   filter(
     across(1, is.na)
   )
# A tibble: 1 x 2
  x         y
  <chr> <int>
1 NA        2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.