I have some data in the format
test <- c(10,11,12,13.1,13.2,13.3,14,15)
The integer count is equivalent to counting units of 4 subunits, and as such the 13.1 etc. do not mean 13-and-one-tenth but are instead 13-and-one-quarter (i.e. the count from 1 to 2 would be 1, 1.1, 1.2, 1.3, 2, etc...) I am attempting to prepare plots of these, and therefore to get the spacing correct I think I need to change them such that x.1 becomes x.25, x.2 becomes x.5 and x.3 becomes x.75. I could expand the integers but that will cause other issues down the line I think, so I'd prefer to do the .25/.5/.75 conversion.
I'm attempting to do this with switch()
and not getting the expected result, so I guess my questions are twofold:
- Why isn't this
switch()
working as expected? - What is the better way to do this that I have clearly missed?
I have tried to take the modulo 1 of the input, giving me just the .0, .1 etc, multiply by 10 and add 1 to get a number 1-4 and use that to select the relevant case option from the switch()
statement as below.
Desired outcome:
[1] 10.00 11.00 12.00 13.25 13.5 13.75 14.00 15.00
So the 13.1 and 13.2 terms seem not be evaluating as I expect.
# testing modulo approach
sapply(test, FUN = \(x) {
(x %% 1)*10+1
}
)
[1] 1 1 1 2 3 4 1 1
and then
sapply(test, FUN = \(x) {
switch((x %% 1)*10+1,
x, # option 1
floor(x) + 0.25, # option 2
floor(x) + 0.5, # option 3
floor(x) + 0.75) # option 4
}
)
[1] 10.00 11.00 12.00 13.10 13.25 13.75 14.00 15.00