0

what is the difference between ==1 and ==-1 in these following codes?

mydata1=data.frame(State=ifelse(sign(rnorm(6))==-1,"Mina","Mani"),Q1=sample(1:6))
mydata2= data.frame(State=ifelse(sign(rnorm(6))==1,"Mina","Mani"),Q1=sample(1:6))
Dee_wab
  • 1,171
  • 1
  • 10
  • 23
Vin
  • 19
  • 2
  • The code is just assigning 'Mani' and 'Mina' randomly with equal probability. There is no significant difference. – Rohit Jun 20 '18 at 09:56

1 Answers1

0

the -1 is just an inversion of the TRUE/FALSE operator in your code. You can reproduce your code with set.seed(x)

Sign() produces you 1 and -1, so if you take your example:

sign(rnorm(6))

[1] -1 -1 1 1 1 1

so if you take the equal operator, you just ask if it's -1 or 1. Actually you want to generate randomly the names in your code, so it doesn't make any difference. But for understanding here what happens if you switch the numbers:

set.seed((123))
sign(rnorm(6))==-1

[1] TRUE TRUE FALSE FALSE FALSE FALSE

set.seed((123))
sign(rnorm(6))==1

[1] FALSE FALSE TRUE TRUE TRUE TRUE

with this information your code proceeds

mischva11
  • 2,811
  • 3
  • 18
  • 34