r - How to calculate the amount of numbers inside a specific range -
i'm still having problems calculating numbers.
trying find amount of numbers inside [-0.5 , 0.5] first line, , amount of numbers outside same range in second line.
i use abc = rnorm(100, mean=0, sd=1)
. have 100 numbers in total, have 35 numbers inside range, , 35 outside range, dosen't add 100.
length(abc[abc>=-0.5 & abc<=0.5]) [1] 35 length(abc[abc<-0.5 & abc>0.5]) [1] 35
then tried:
length(which(abc>=-0.5 & abc<=0.5)) [1] 40 length(which(abc<-0.5 & abc>0.5)) [1] 26
and still doesn't add 100. what's wrong?
you after:
r> set.seed(1) r> abc = rnorm(100, mean=0, sd=1) r> length(abc[abc >= -0.5 & abc <= 0.5]) [1] 41 r> length(abc[abc < -0.5 | abc > 0.5]) [1] 59
what went wrong
two things:
abc < -0.5 & abc > 0.5
asking values less -0.5 and greater 0.5however, had:
abc[abc<-0.5 & abc>0.5]
bit different due scoping. let's pull apart:r> abc[abc<-0.5 & abc>0.5] [1] 1.5953 0.7383 0.5758 1.5118 1.1249 0.9438 <snip>
now let's @
abc
r> abc [1] false false false true false false false
you've changed value of
abc
! because<-
assignment operator. have setabc
equal0.5 & abc > 0.5
. avoid this, use spacing (as in code).
Comments
Post a Comment