A typical reason is when there is a "hidden" missing value. That's when, in the long format, there is no apparent missing value, but there is a particular combination of variable that doesn't appear. Then when converting to wide format, the value for that combination is not known.
Modifying the example in ?longToWide
:
long <- data.frame(
id = c(1, 2, 1, 2, 3, 1, 2, 3),
time = c("t1", "t1", "t2", "t2", "t2", "t3", "t3", "t3"),
accuracy = c(.50, .03, .94, .63, .49, .78, .71, .16)
)
long
#> id time accuracy
#> 1 1 t1 0.50
#> 2 2 t1 0.03
#> 3 1 t2 0.94
#> 4 2 t2 0.63
#> 5 3 t2 0.49
#> 6 1 t3 0.78
#> 7 2 t3 0.71
#> 8 3 t3 0.16
lsr::longToWide(long, accuracy ~ time)
#> id accuracy_t1 accuracy_t2 accuracy_t3
#> 1 1 0.50 0.94 0.78
#> 2 2 0.03 0.63 0.71
#> 3 3 NA 0.49 0.16
Created on 2023-09-21 with reprex v2.0.2
In this case you see a missing value in wide but not long format, that's because the long format does not contain any value for time == t1
and id == 3
.
If this is not what happens in your case, you will need to share some example data to reproduce your problem, as detailed here.