# Digits - purrr package r studio

Hello, I am struggling with finding one code related to digitsNow write a function named `swap` that

• takes an integer vector `vec` as input,
• swaps every odd-even positional pairs (of digits)

here are my findings until now :

a <- 1:100
n <- length(a)
if (i %% n ≠ 0)
for (i in length(a))
{i <- 1
j <- a[i]
g <- a[i+1]
a[i] <- g
a[i+1] <- j }}

can someone help me?
thank you

``````# FUNCTIONS

is_even <- function(x) x%%2 == 0
is_hit  <- function(x) is_even(x[1]) & !is_even(x[2]) | !is_even(x[1]) & is_even(x[2])
gen_data <- function(x) sample(seq(from = 1, to = x),replace = TRUE
mk_pairs <- function(x) split(x, ceiling(seq_along(x)/2))
prepare <- function(x) mk_pairs(gen_data(x))
exchange <- function(x) if(is_hit(x)) rev(x) else x
swap <- function(x) unlist(lapply(prepare(x), exchange), recursive = FALSE, use.names = FALSE)

# MAIN

set.seed(137)
swap(100)
#>   [1] 34 59  8 38 79 39 96 93 35 65 30 48 13 43 14 22  4  6 89 70 48 15 13 32 14
#>  [26] 95 35 97 55 35 86 35 47 16 20 15 71 96 13 14 69 64 51 99 70 91 38 87 54 65
#>  [51] 38 47  8 44 25 96 22  9 69 91 58 30 21 55 89 45 73 50  3 18 46 96 36 31 44
#>  [76] 60 78 55 15 89  1 89 27 94 59 43 44 35 40 83 43 40 81 14 46 37 43 10  7 59
set.seed(137)
unlist(prepare(100), recursive = FALSE, use.names = FALSE)
#>   [1] 59 34  8 38 79 39 93 96 35 65 30 48 13 43 14 22  4  6 70 89 15 48 32 13 95
#>  [26] 14 35 97 55 35 35 86 16 47 15 20 96 71 14 13 64 69 51 99 91 70 87 38 65 54
#>  [51] 47 38  8 44 96 25  9 22 69 91 58 30 21 55 89 45 50 73 18  3 46 96 31 36 44
#>  [76] 60 55 78 15 89  1 89 94 27 59 43 35 44 83 40 40 43 14 81 37 46 10 43  7 59
``````

Created on 2020-10-17 by the reprex package (v0.3.0.9001)

this is not correct according to my R studio.. To check the answer:

• The output of `print(swap(1:10))` is

[1] 2 1 4 3 6 5 8 7 10 9

swap(x) takes an int, not a vector

I need a swap function that takes an integer vector vec as input, and swaps every odd-even positional pairs of digits.

The previous question was about Setting the random seed as 0 and save the random seed vector into a variable seed whose answer was :

set.seed(0)
seed <- set.seed(0)
seed <- .Random.seed
seed

`swap` takes an integer as a matter of convenience for illustrating the compositioning of a solution in the absence of any representative data in the `reprex`. `gen_data` and `prepare` can be dropped and `mk_pairs` substituted for `prepare` in `swap`.

`set.seed` was used solely for the purpose of a reproducible example showing both the swapped vector and the source vector. It was not there to address any question outside the thread..

``````nums <- 1:19

(numtext <- paste0(nums,collapse = "") )
(digitstext2 <- digitstext <- unlist(strsplit(numtext,"?")))

pairlength <- 2*floor(nchar(numtext)/2)
(pos0 <- which(seq_len(pairlength) %% 2 == 0))
(pos1 <- which(seq_len(pairlength) %% 2 == 1))
digitstext2[pos0] <- digitstext[pos1]
digitstext2[pos1] <- digitstext[pos0]

digitstext
digitstext2
``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.