-1

INITIAL SETUP

# turning MHID9870 into a bit string
en <- utf8ToInt("MHID9870")
en <- binary(en, mb=7)
en <- strsplit(en,"")
en <- unlist(en)
en <- as.integer(en)
en

# Separating the bit string into parts
parts <- en
x <- parts[1:19]
y <- parts[20:41]
z <- parts[21:64]

# Finding frequency of each binary bit
my_mode <- function(v){
  freq_count <- table(v)
  max_idx <- which.max(freq_count)
  m <- names(max_idx)
  return(m)
}
xyzmaj <- my_mode(c(x[9],y[11],z[11]))

QUESTION STARTS BELOW:

I created the following if-else and for loop statement

variable names: xyzmaj = is the majority bit (0 or 1) between the x,y,z strings my_mode = is a function that takes a string and returns the mode of the string. The variable "x" is a 19 bit long string of bit numbers (0 and 1)

if ( xyzmaj == my_mode(x)) {
  for(i in 1:length(x)){
    x[i+1] <- x[i]
    i <- i+1
  }
}
x

my output keeps showing up as 
000000000000000000000

I am trying to make it shift right, but the code keeps just taking the first integer and replacing with every integer after.

So basically if X is 010011010101001000010

Output should be 001001101010100100001

Zaid Islam
  • 29
  • 3
  • 2
    You should share everything needed to run your code. Also show expected output. – markus Feb 26 '20 at 20:26
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 26 '20 at 20:32
  • Where does this `binary()` function come from? I can't find one that has an `mb=` parameter. Where are you defining `xyzmaj`? – MrFlick Feb 26 '20 at 20:56
  • I think it is `compositions::binary()` – Till Feb 26 '20 at 21:04
  • 1
    Is xyzmaj an actual number, or a string in the form of "0" or "1"? The output of my_mode is a character, not a number, so comparing the two will always be false if xyzmaj is numeric – Allan Cameron Feb 26 '20 at 21:14
  • incidentally, my_mode could be written as `my_mode <- function(v) as.character(round(sum(v)/length(v)))` – Allan Cameron Feb 26 '20 at 21:17

1 Answers1

1

Assuming that you are getting the binary() function from the compositions package, this should work. I wasn't able to recreate your code completely, since you didn't share how the xyzmaj object is created.

if ( xyzmaj == my_mode(x)) {
  x <- c(x[length(x)], x[1:length(x)-1])
}
x

This cuts off the last entry of the x vector and puts it in as the first entry, making the previous first entry the second etc.

EDIT: Some explanation, why your for loop solution is not giving you the result you expected:

Since you are changing x in the for loop, the first value in the vector is just getting propagated in the whole vector, as by the second iteration x[i] has been set to 0 in the first iteration and so on. This issue could be resolved by cycling though x backwards instead of forward. In addition to that you would still have to make sure that the last entry is used as the first entry, for which you have to store x in a separate object, before you start changing x. This could look something like this:

  old_x <- x
  for(i in length(x):2){
    x[i] <- x[i - 1]
  }
  x[1] <- old_x[length(old_x)]
  x
Till
  • 3,845
  • 1
  • 11
  • 18