2015-05-09 70 views
1

我有以下數據如何計算R中區間內外的數據比例?

Frequency = 260 



[1] -9.326550e-03 
    [2] -4.422175e-03 
    [3] 9.003794e-03 
    [4] -1.778217e-03 
    [5] -4.676712e-03 
    [6] 1.242704e-02 
    [7] 5.759863e-03 

而且我想算多少,這些都在這兩者之間:

Frequency = 260 



      [,1]   [,2] 
[1]   NA   NA 
[2] 0.010363147 -0.010363147 
[3] 0.010072569 -0.010072569 
[4] 0.010018997 -0.010018997 
[1] 0.009700522 -0.009700522 
[5] 0.009476024 -0.009476024 
[7] 0.009748085 -0.009748085 

我不得不這樣做在R,但我是一個初學者。 在此先感謝!

+3

你需要做的第一件事就是呈現一個R對象。該輸出並不表明你還沒有對R做過任何事情。這不是打印操作的典型結果。製作一個對象並在R代碼中顯示所需的間隔中斷。 –

+0

他們是我認爲的時間系列對象 – user137425

+0

你的範圍總是對稱零?在這種情況下,與絕對值比較將是最簡單的。 – Frank

回答

3

除非我誤解 - 您希望第一個對象的第j個元素在第二個第j行的兩個元素之間的次數是多少?如果是這樣,

sum((data1 > data2[,1]) & (data1 < data2[,2]))/length(data1) 

會這樣做。

+0

的輸出一起呈現給我們嗯,我肯定讀了OP的問題,我猜想他們只能澄清。 :) –

+0

是的,我想要那個!謝謝! – user137425

+2

需要注意的一件事是,如果您的數據實際上確實存在NA值的第一個範圍(或其他),那麼@ Carl的解決方案將不起作用。你需要添加'na.rm = T'參數到sum:'sum(d> r [,1]&d

3

下面是一個使用foverlaps從包裝data.table,用下面的玩具數據集的一種方法:

library(data.table) 
## 
set.seed(123) 
ts1 <- data.table(
    ts(rnorm(50, sd = .1), frequency = 260))[ 
    ,V2 := V1] 
## 
ts2 <- cbind(
    ts(rnorm(50,-0.1,.5), frequency=260) 
    ,ts(rnorm(50,0.1,.5), frequency=260)) 
ts2 <- data.table(
    t(apply(ts2, 1, sort)))[ 
    1, c("V1", "V2") := NA] 
setkeyv(ts2, c("V1","V2")) 

由於foverlaps從每個輸入data.table S的需要兩列,我們只是複製了第一列ts1(就我所知,這是慣例)。

fts <- foverlaps(
    x = ts1, y = na.omit(ts2) 
    ,type = "within")[ 
    ,list(Freq = .N) 
    ,by = "V1,V2"] 

這對加入ts2ts1ts1值中的每個ts2[V1, V2]區間落在每一個發生 - 然後聚集由間隔獲得計數。由於某些ts2的間隔可能包含零ts1值(這是此示例數據的情況)是可行的,因此您可以將彙總數據留在原始ts2對象上,並得出相應的比例:

(merge(x = ts2, y = fdt, all.x=TRUE)[ 
    is.na(Freq), Freq := 0][ 
    ,Inside := Freq/nrow(ts1)][ 
     ,Outside := 1 - Inside])[1:10,] 
## 
#   V1   V2 Freq Inside Outside 
# 1:   NA   NA 0 0.00 1.00 
# 2: -1.2545844 -0.37373731 0 0.00 1.00 
# 3: -0.9266236 -0.21024328 1 0.02 0.98 
# 4: -0.8743764 -0.29245223 0 0.00 1.00 
# 5: -0.7339710 0.19230687 50 1.00 0.00 
# 6: -0.7103589 0.13898042 50 1.00 0.00 
# 7: -0.7089414 -0.26660369 0 0.00 1.00 
# 8: -0.7007681 0.58032622 50 1.00 0.00 
# 9: -0.6860721 0.01936587 35 0.70 0.30 
# 10: -0.6573338 -0.41395304 0 0.00 1.00 
2

我認爲@ nrussell的答案很好,但是您可以更簡單地使用base R來完成您的答案,所以我會在此爲您記錄它,因爲您說您是初學者。我已經評論過它,希望能幫助你瞭解發生了什麼:

## Set a seed so simulated data can be duplicated: 
set.seed(2001) 

## Simulate your data to be counted: 
d <- rnorm(50) 

## Simulate your ranges: 
r <- rnorm(10) 
r <- cbind(r - 0.1, r + 0.1) 

## Sum up the values of d falling inside each row of ranges. The apply 
## function takes each row of r, and compares the values of d to the 
## bounds of your ranges (lower in the first column, upper in the second) 
## and the resulting logical vector is then summed, where TRUEs are equal 
## to 1, thus counting the number of values in d falling between each 
## set of bounds: 
sums <- apply(r, MARGIN=1, FUN=function(x) { sum(d > x[1] & d < x[2]) }) 

## Each item of the sums vector refers to the corresponding 
##  row of ranges in the r object...