I'm running into a behavior of pmax
that I can't quite understand:
pmax(data.frame(matrix(1:16, nrow=4)), c(6))
would return
X1 X2 X3 X4
1 6 NA 9 13
2 6 6 10 14
3 6 7 11 15
4 6 8 12 16
What I don't understand is why only the entries smaller than 6 and are not in the first column got turned into NA
- if we recycle c(6, NA, NA, NA)
through the rows, wouldn't all of X2
, X3
, X4
be NA
since max(NA, anything) = NA
? Why only the entries that are not in the first column and is smaller than 6 is changed into NA
s?
pmax
is not designed to be used with data.frame input.
The error is introduced in line 35 of pmax
:
mmm[change] <- each[change]
because each
is defined to be as long as the length
of the input, which for a data.frame is the number of columns. Therefore when it tries to address the 5th element, it gets NA.
each
[1] 6 6 6 6
each[change]
[1] 6 6 6 6 NA
The obvious workaround is to convert to data.frame after using pmax
:
data.frame(pmax(matrix(1:16, nrow=4), c(6)))
X1 X2 X3 X4
1 6 6 9 13
2 6 6 10 14
3 6 7 11 15
4 6 8 12 16
Or convert back and forth as required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With