2017-06-28 14 views
1

在一個數據幀。我想比較今天的價值與回顧'n'日期。的R - =如果最大語句從Excel至R

我知道如何做到這一點在Excel中比較今天的價值,看它是否比前10天更高。

=IF(A11>MAX(A1:A10),1,0) 

我該怎麼辦R中的功能在同一個邏輯?

輸出是這樣的下面:

Column Output 
1 12 NA 
2 13 NA 
3 14 NA 
4 15 NA 
5 9  NA 
6 9  NA 
7 7  NA 
8 8  NA 
9 16 NA 
10 17 NA 
11 20 1 
12 14 0 
13 9  0 
14 8  0 
15 6  0 
16 5  0 
17 28 1 

在行11.由於值20比前10天更高它表示一個1個值。

在列12中,因爲值14不是最高編號在以前10天它接收到一個0值。

它當然軋輥上移動10天窗口和。

回答

4

P·拉普安特的答案是偉大的,但anyti我正在做一個「滾動」計算,我的第一個直覺是從動物園包中想到rollapply

is_last_greatest <- function(x){ 
    #' Given an input vector this returns 
    #' 1 if the last element is greater than 
    #' all of the other elements and 0 otherwise 
    ifelse(all(tail(x,1) > head(x,-1)), 1, 0) 
} 

# We want to compare to the previous 10 values but the function 
# I wrote requires us to include the value we're using as 
# comparison so I set the width to 11 
output <- rollapply(dat, 
      width = 11, 
      FUN = is_last_greatest, 
      fill = NA, 
      align = "right") 

cbind(dat, output) 

這給

 dat vals 
[1,] 12 NA 
[2,] 13 NA 
[3,] 14 NA 
[4,] 15 NA 
[5,] 9 NA 
[6,] 9 NA 
[7,] 7 NA 
[8,] 8 NA 
[9,] 16 NA 
[10,] 17 NA 
[11,] 20 1 
[12,] 14 0 
[13,] 9 0 
[14,] 8 0 
[15,] 6 0 
[16,] 5 0 
[17,] 28 1 
+0

好吧,如果數據集中有缺失的值,那麼我可以在roll apply中添加這個值? na.rm = TRUE,或者我們可以讓它繼續滾動,如果我們缺少值... –

+1

它將執行您要調用的函數指定的任何操作。所以,如果你使用rollapply做了一個意思,你可以傳遞na.rm = TRUE,如果這是你想要的。 – Dason

3

以下是如何從RcppRoll做到這一點與roll_maxr

library(RcppRoll) 
df$Output2 <- ifelse(df$Column>roll_maxr(lag(df$Column),11, na.rm = TRUE),1,0) 

    Column Output Output2 
1  12  NA  NA 
2  13  NA  NA 
3  14  NA  NA 
4  15  NA  NA 
5  9  NA  NA 
6  9  NA  NA 
7  7  NA  NA 
8  8  NA  NA 
9  16  NA  NA 
10  17  NA  NA 
11  20  1  1 
12  14  0  0 
13  9  0  0 
14  8  0  0 
15  6  0  0 
16  5  0  0 
17  28  1  1 

數據

df <- read.table(text=" Column Output 
1 12 NA 
       2 13 NA 
       3 14 NA 
       4 15 NA 
       5 9  NA 
       6 9  NA 
       7 7  NA 
       8 8  NA 
       9 16 NA 
       10 17 NA 
       11 20 1 
       12 14 0 
       13 9  0 
       14 8  0 
       15 6  0 
       16 5  0 
       17 28 1",header=TRUE,stringsAsFactors=FALSE) 
+1

@AndrewBannerman我看到你的問題的另一種回答關於採取來港定居的照顧。我在我的答案中添加了',na.rm = TRUE'。 –

1

下面是使用embed構造比較組和與apply執行所述比較的基礎R法。

# get a matrix for comparisons 
myMat <- embed(df$Column, 11) 

在這裏,這將返回

myMat 
    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] 
[1,] 20 17 16 8 7 9 9 15 14 13 12 
[2,] 14 20 17 16 8 7 9 9 15 14 13 
[3,] 9 14 20 17 16 8 7 9 9 15 14 
[4,] 8 9 14 20 17 16 8 7 9  9 15 
[5,] 6 8 9 14 20 17 16 8 7  9  9 
[6,] 5 6 8 9 14 20 17 16 8  7  9 
[7,] 28 5 6 8 9 14 20 17 16  8  7 

所以我們的目標是值了與在每一行的剩餘列的第一列進行比較。現在

as.integer(max.col(myMat) == 1L) 
[1] 1 0 0 0 0 0 1 

,在NA值的適當數量,這是在myMat減一列數粘性。

df$output2 <- c(rep(NA, ncol(myMat) - 1), as.integer(max.col(myMat) == 1L)) 

這將返回

df 
    Column Output output2 
1  12  NA  NA 
2  13  NA  NA 
3  14  NA  NA 
4  15  NA  NA 
5  9  NA  NA 
6  9  NA  NA 
7  7  NA  NA 
8  8  NA  NA 
9  16  NA  NA 
10  17  NA  NA 
11  20  1  1 
12  14  0  0 
13  9  0  0 
14  8  0  0 
15  6  0  0 
16  5  0  0 
17  28  1  1 

max.col一個優點是,它是相當快的。其最大的缺點之一是它沒有na.rm參數來刪除缺少的值。在缺少值的情況下,這裏是在myMat上使用apply而不是max.col的方法。

apply(myMat, 1, function(x) as.integer(all(head(x, 1) > tail(x, -1)))) 

操作比較函數這裏是產生相同的結果

all(head(x, 1) > tail(x, -1)) 

功能包括以下

head(x, 1) == max(x) # or 
x[1] == max(x) 

1L == which.max(x) 
+1

我不知道'embed'函數。這看起來可能有用。謝謝! – Dason