如何将数值变量与分位数结果进行比较?

时间:2014-10-02 17:00:56

标签: r if-statement quantile

如果数字变量介于两个分位数值之间或外部,我想打印一个语句。我在Mac上使用R studio

Under5<- quantile(Vector, probs=0.05)
Above95 <- quantile(Vector, probs=c(0.95))

if (Under5<Varaible<Above95){
  print("Inbetween")
} else {
  print("Outside")
}

1 个答案:

答案 0 :(得分:0)

获取清单:

> ifelse(vect>Above95 | vect<Under5, 'Outside', 'InBetween')
  [1] "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "Outside"   "InBetween" "InBetween" "InBetween"
 [11] "InBetween" "InBetween" "InBetween" "InBetween" "Outside"   "InBetween" "Outside"   "InBetween" "InBetween" "InBetween"
 [21] "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween"
 [31] "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "Outside"   "InBetween" "InBetween" "InBetween"
 [41] "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween"
 [51] "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "Outside"   "InBetween" "InBetween"
 [61] "InBetween" "Outside"   "InBetween" "Outside"   "InBetween" "InBetween" "InBetween" "Outside"   "InBetween" "InBetween"
 [71] "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween"
 [81] "Outside"   "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween"
 [91] "InBetween" "Outside"   "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween" "InBetween"

打印报表:

> for(x in vect) 
      if(x>Above95 || x<Under5) print('Outside') else print('InBetween') 
[1] "InBetween"
[1] "InBetween"
[1] "InBetween"
[1] "InBetween"
[1] "InBetween"
[1] "InBetween"
[1] "Outside"
[1] "InBetween"
[1] "InBetween"
[1] "InBetween"
[1] "InBetween"
[1] "InBetween"
.....

数据:

 vect = rnorm(100)
> vect
  [1] -0.19499248  0.39192391 -1.23743256  0.09378565 -1.59986484  1.59367729  1.75754200  0.65658467  0.34199444  1.53622220
 [11]  0.27041005 -0.20428215  0.23016708 -0.48747233 -2.02944783  0.20097530  1.67626261 -0.08633930  0.58775014  1.05145409
 [21]  0.69660196 -0.90927802  0.98798529  1.03437030  0.69491892  1.42815799  0.58344582  1.05999840  0.78323746  0.03575642
 [31] -0.13879192 -1.07342707 -1.39490368  0.88512004  1.11613360  0.79637034  1.94720723  0.20585125 -0.19382803  1.09700134
 [41]  0.64202202 -1.54540900 -0.78017510 -0.87482947  0.14790355  0.52039545 -0.53557875  0.72955954  0.43591900 -0.02105778
 [51]  1.51194451  0.85792473 -0.53723797  0.64897382  0.32405030 -1.46600906  0.56788307  1.99545382  1.58474734 -0.02482142
 [61] -0.94013025 -2.47879471  0.56555899 -1.89259690  1.18141740 -0.13438659 -0.81825495 -1.96651954  0.83596248 -1.00502859
 [71]  0.10408941  0.67101069 -0.64285599 -0.87825821 -0.56323413  0.51264863  0.57388733  0.50831974  0.23835946 -1.50733920
 [81]  1.64445645 -0.28288487 -0.94753033 -0.18220994  0.91416724  0.57851873  0.18967501 -0.18647597 -0.49192116 -1.15468615
 [91] -0.03190201 -1.97962013 -0.28199870  0.39037380  0.28339627 -0.85271110 -1.55401361  0.87012128 -0.30605918 -0.90286082