I try to do something like
df[<very-long-and-complicated-selection>,]$foo <- "bar"
This works well, if there are rows matching the selection.
If not I get an error message
Error in
$<-.data.frame
(*tmp*
, "foo", value = "bar") :
replacement has 1 row, data has 0
However, my code is designed in a way, where no matching is possible.
Is there a clean, short and easy solution to avoid these (and only these) errors?
Use
df[<very-long-and-complicated-selection>, "foo"] <- "bar"
ie, make the assignment into the dataframe treating it as a 2-dimensional object, rather than as a list.
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