I am dealing with strings having two separators "*" and "|", and they are used in strings such as:
"3\*4|2\*7.4|8\*3.2"
Where the number right before "*" denotes frequency and the float or integer right after "*" denotes value. These value frequency pairs are separated using "|".
So from "3\*4|2\*7.4|8\*3.2", I would like to get a following vector:
"4","4","4","7.4","7.4","3.2","3.2","3.2","3.2","3.2","3.2","3.2","3.2"
I have come up with following syntax, which completes with no errors and warnings, but the end results something else than expected:
strsplit("3*4|2*7.4|8*3.2", "[*|]") %>% #Split into a vector with two different separator characters
unlist %>% #strsplit returns a list, so let's unlist it
mapply(FUN = rep,
x = .[seq(from = 2, to = length(.), by = 2)], #these sequences mean even and odd index in this respect
times = .[seq(from = 1, to = length(.), by = 2)], #rep() flexibly accepts times argument also as string
USE.NAMES = FALSE) %>%
unlist #mapply returns a list, so let's unlist it
[1] "4" "4" "4" "7.4" "7.4" "7.4" "7.4" "3.2" "3.2" "4" "4" "4" "4" "4" "4" "4" "7.4" "7.4" "7.4" "7.4" "7.4" "7.4" "7.4" "7.4" "3.2" "3.2" "3.2"
As you can see, something weird has happened. "4" has been repeated three times, which is correct, but "7.4" has been repeated four times (incorrectly) and so on.
What is going on here?
1a) The problem with the code in the question is that %>% is passing dot to the first argument of
mapplyTo avoid this replace themapplylines with this where...represents the same arguments as in the question.1b) Actually
mapplyis not needed in the first place sincerepis vectorized:1c) and a further simplification is to use logical values for the index realizing that they recycle:
1d) A base R version using R's pipes is:
Also note the following one-liners:
2a) The following one-liner matches the two numbers and passes them as separate arguments to the anonymous function specified using formula notation returning the output of the function. The input
xis from the question and defined explicitly in the Note at the end.2b) If we have a character vector of strings like
xthen it will also work by removing the[[1]]. In that case it will return a list of the results.3) Another way to do it is to extract the repetition numbers and the values separately and pass each such vector to
rep.Note
The input used is: