Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setv() typeof coercion #498

Closed
tony-aw opened this issue Nov 15, 2023 · 4 comments
Closed

setv() typeof coercion #498

tony-aw opened this issue Nov 15, 2023 · 4 comments

Comments

@tony-aw
Copy link

tony-aw commented Nov 15, 2023

Hi,

First of all: I enjoy the collapse package very much. Thank you for your efforts, it is greatly appreciated!

Regarding my question/request...

Consider the following code:

x <- 1:16
collapse::setv(x, as.integer(1:8), R = 8.5, vind1 = TRUE)
x

What I would expect x to become is c(rep(8.5, 8), 9:16).
But what I get is c(rep(8, 8), 9:16).
If I explictly set as.double(x) , this gets corrected.
I get why this is: no typeof coercion takes place. But I can imagine the above situation can lead to problems, especially in large scripts.
It would be nice if either setv() gives an Error or Warning when R and x are not of the same type, or if setv() allows for type coercion just like base R's [<- method, though the latter might not be possible.

Kind regards,

Tony.

@SebKrantz
Copy link
Owner

SebKrantz commented Nov 15, 2023

Hi, thanks. But it’s again the same answer as your last issue: operations by reference can neither change the type nor length of a vector. To change the type the entire vector would need to be copied to a larger double vector of the same length, but this vector needs to be allocated and would need to be assigned to a name e.g. x or y etc. This is not an operations by reference. By reference means we simply access and modify data structures without changing anything in the way these are stored and referenced in computer memory. So what you are saying is expected. The digits are simply ignored because the integer vector cannot accommodate them. Note that in base R you get the same behavior e.g. x[1:8] <- 8.5 should remove the digit (at least I think this is also a by-reference operation). If not then <- is not by reference and this operation first copies x to a double vector and then does the replacement (which is very inefficient).

@tony-aw
Copy link
Author

tony-aw commented Nov 15, 2023

Hi, suspected it was not possible (as I stated myself). That's why I asked if an error or warning could be produced when the user attempts to perform a replacement where the replacement R is of different type than x.
So, is it possible to give a warning or error?

PS: [<- is not by reference. It will coerce x if the replacement is of different type.

@SebKrantz
Copy link
Owner

I don’t think a warning should be issued because this is the expected behavior, and the input could be cleanly coercible e.g. using 8 or 8.0 would work without loss of information. But I’ll think about it for a bit.

@tony-aw
Copy link
Author

tony-aw commented Nov 15, 2023

OK. Btw, data.table::set() does give warnings, so it would be more consistent with data.table if collapse::setv() also gives warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants