1

##This is the code that I ran in R. I run this and there is no output anywhere at all. I cant figure out why.

polarizerset <- function(x){
      fw <- c()
      ps <- c(0,1)
      sw <- sample(x, 1, prob = c(0.5,0.5))
      sp <- sample(ps, 1, prob = c(0.5,0.5))
      if( xor(sw,sp) == FALSE ) {
        fw <- c(fw, as.numeric(!sw), as.numeric(!sw))
      } else {
        fw <- c(fw, NULL, NULL)
        }
    }
Phil
  • 7,287
  • 3
  • 36
  • 66
Ayush Raj
  • 11
  • 1
  • Please make your code complete (needs a sample call to test it that shows what is the problem) and reproducible (need to use `set.seed` before it since there are random numbers used. Also provide some description a of what i it is intended to do. See instructions at top of [tag:r] tag page. – G. Grothendieck Sep 18 '20 at 15:04

1 Answers1

0

There's a result, but it's returned invisibly due to the <- operator:

polarizerset <- function(x){
  fw <- c()
  ps <- c(0,1)
  sw <- sample(x, 1, prob = c(0.5,0.5))
  sp <- sample(ps, 1, prob = c(0.5,0.5))
  if( xor(sw,sp) == FALSE ) {
    fw <- c(fw, as.numeric(!sw), as.numeric(!sw))
  } else {
    fw <- c(fw, NULL, NULL)
  }
}

res <- polarizerset(2)
res

[1] 0 0

See this link which shows why.
If you want to see the result, just remove the fw <- which isn't useful in the function:

polarizerset <- function(x){
  fw <- c()
  ps <- c(0,1)
  sw <- sample(x, 1, prob = c(0.5,0.5))
  sp <- sample(ps, 1, prob = c(0.5,0.5))
  if( xor(sw,sp) == FALSE ) {
    c(fw, as.numeric(!sw), as.numeric(!sw))
  } else {
    c(fw, NULL, NULL)
  }
}
polarizerset(2)
[1] 0 0
Waldi
  • 39,242
  • 6
  • 30
  • 78