Exchange empty cells for the number 0 in my database

How do I exchange empty cells for the number 0 in my database below. I know it is possible to do: df1[df1==""]<-0, in this case it checks all columns in my database, however I would like it to be checked from column DR01. I thought of something like this:
df1 %>% select(starts_with("DR0")=="")<-0, but it didn't work.

df1 <- structure(
  list(Date = c("30-06-2021","01-07-2021","02-07-2021","03-07-2021"),
       DR1 = c(4,1,4,1),
       DR01 = c(4,1,4,2), DR02= c(4,2,6,2),DR03= c(9,5,4,2),
       DR04 = c(5,4,3,3),DR05 = c(5,4,5,""),
       DR06 = c(2,4,3,""),DR07 = c(2,5,4,""),
       DR08 = c(0,0,0,""),DR09 = c(0,0,0,""),DR010 = c(0,0,0,""),DR011 = c(4,0,0,""), 
       DR012 = c(0,0,0,""), DR013 = c(0,0,1,""), DR014 = c(0,0,0,"")),
  class = "data.frame", row.names = c(NA, -4L))
> df1
        Date DR1 DR01 DR02 DR03 DR04 DR05 DR06 DR07 DR08 DR09 DR010 DR011 DR012 DR013 DR014
1 30-06-2021   4    4    4    9    5    5    2    2    0    0     0     4     0     0     0
2 01-07-2021   1    1    2    5    4    4    4    5    0    0     0     0     0     0     0
3 02-07-2021   4    4    6    4    3    5    3    4    0    0     0     0     0     1     0
4 03-07-2021   1    2    2    2    3

Generally, "" should be converted to NA, not to 0

suppressPackageStartupMessages({
  library(dplyr)
  library(lubridate)
})

df1 <- data.frame(
  Date = c("30-06-2021", "01-07-2021", "02-07-2021", "03-07-2021"),
  DR1 = c(4, 1, 4, 1),
  DR01 = c(4, 1, 4, 2), DR02 = c(4, 2, 6, 2), DR03 = c(9, 5, 4, 2),
  DR04 = c(5, 4, 3, 3), DR05 = c(5, 4, 5, ""),
  DR06 = c(2, 4, 3, ""), DR07 = c(2, 5, 4, ""),
  DR08 = c(0, 0, 0, ""), DR09 = c(0, 0, 0, ""), DR010 = c(0, 0, 0, ""), DR011 = c(4, 0, 0, ""),
  DR012 = c(0, 0, 0, ""), DR013 = c(0, 0, 1, ""), DR014 = c(0, 0, 0, "")
)

# convert date strings to date objects (optional)

df1[,1] <- dmy(df1[,1])     

# convert character variables (those with "blanks") to numeric

make_int <- function(x,y) x[,y] <- as.integer(x[,y])

# inspect column types

tibble(df1)
#> # A tibble: 4 × 16
#>   Date         DR1  DR01  DR02  DR03  DR04 DR05  DR06  DR07  DR08  DR09  DR010
#>   <date>     <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 2021-06-30     4     4     4     9     5 "5"   "2"   "2"   "0"   "0"   "0"  
#> 2 2021-07-01     1     1     2     5     4 "4"   "4"   "5"   "0"   "0"   "0"  
#> 3 2021-07-02     4     4     6     4     3 "5"   "3"   "4"   "0"   "0"   "0"  
#> 4 2021-07-03     1     2     2     2     3 ""    ""    ""    ""    ""    ""   
#> # … with 4 more variables: DR011 <chr>, DR012 <chr>, DR013 <chr>, DR014 <chr>

# all except first are type dbl or chr; 

# change to type int

for(i in 2:16) df1[,i] = make_int(df1,i)

df1
#>         Date DR1 DR01 DR02 DR03 DR04 DR05 DR06 DR07 DR08 DR09 DR010 DR011 DR012
#> 1 2021-06-30   4    4    4    9    5    5    2    2    0    0     0     4     0
#> 2 2021-07-01   1    1    2    5    4    4    4    5    0    0     0     0     0
#> 3 2021-07-02   4    4    6    4    3    5    3    4    0    0     0     0     0
#> 4 2021-07-03   1    2    2    2    3   NA   NA   NA   NA   NA    NA    NA    NA
#>   DR013 DR014
#> 1     0     0
#> 2     0     0
#> 3     1     0
#> 4    NA    NA

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.