How to reshape from long to wide format filling NAs with zeros
05:37 20 Apr 2026

I have a long format table which already has NAs:

df <- data.frame(
  ID = c(1, 1, 2, 2, 3),
  region = c("A", "B", "A", "B", "A"),
  status = c(1, NA, 1, 1, NA)
)

I want to convert to wide format but instead of filling with NAs fill the missing values with zeros to distinguish from the original NAs in long format.

reshape(
  df,
  idvar = "ID",
  timevar = "region",
  direction = "wide"
)

Here for example, since there is no info on ID=3, region=B, it becomes another NA, I'd like that to be a zero instead.

r reshape missing-data long-format-data wide-format-data