Reputation: 75
I'm probably doing something stupid. However, I have a binary vector of length n. I'm trying to then apply an ifelse function depending on selected random values from the vector.
y <- sample(0:1, 500, replace = TRUE)
y
p=0.2
randomcells <- sample(1:length(y), 20)
randomcells
for (i in 1:length(y)){
if(!i %in% randomcells){
next
}
print(y[i])
print(ifelse(y[i]<-1,log((1-p)/p),log((p/1-p))))
}
}
I'd expect from running this code values that are y[i] = 0
to return a negative value else a positive value. Unfortunately, it currently only returns positive values regardless of the value of y[i]
. Any help is greatly appreciated.
Once this is done, I'm then looking to save positive values in one vector and negative values in the other, but those steps are proving difficult due to this current issue.
I really appreciate any help you can provide.
Sam
Upvotes: 0
Views: 29
Reputation: 102710
You also have log((1-p)/p)
since you have y[i] <- 1
as the condition test in your ifelse
statement, which is coerced to logical TRUE
for the condition
ifelse(y[i]<-1,log((1-p)/p),log((p/1-p)))
I guess what you need might be
ifelse(y[i],log((1-p)/p),log((p/1-p)))
or
ifelse(y[i]==1,log((1-p)/p),log((p/1-p)))
Upvotes: 1