簡體   English   中英

跨多列運行移動平均線 - R

[英]Run Moving Average Accross Multiple Columns - R

我正在尋找一種方法來為我的數據框中的每一列執行移動平均計算並將結果附加到原始數據框中

R 沒有循環所有列給我這個我不明白的錯誤

2:在 if (type == "s") { :條件的長度 > 1 並且只使用第一個元素 3:在 colnames(temp_df)[1] <- new_colname :要替換的項目數不是替換長度的倍數

我了解 for 循環適用於邏輯元素,但我不知道如何使用循環來解決這個問題。

    library(lubridate)
    library(pracma)
    library(dplyr)

  # ---- Fake Data Frame

    date       a         b         c                  d                         e
    01/01/2018 0.2087381 0.2892251 0.003478307        0.1186026                 0
    02/01/2018 0.3213273 0.4482369 0.005533266        0.2137506                 0
    03/01/2018 0.2516612 0.3508488 0.004430171        0.1671371                 0
    04/01/2018 0.2436500 0.3375996 0.004323420        0.1580058                 0
    05/01/2018 0.2542939 0.3509883 0.004421275        0.1621509                 0
    06/01/2018 0.2385525 0.3277567 0.004154398        0.1676672                 0


        #add index to allow merge after looping
        rawData <- rawData %>%
          mutate(index = row_number())


        for (i in colnames(rawData)) {

          #define the column in the loop
          data <- rawData[, i]

          #transform to data series
          ts_data <- ts(data, frequency = 365)

          #create mvg avg
          ts_data_mvgavg <- movavg(ts_data,n=7)

          #store result in a data frame
          temp_df <- as.data.frame(ts_data_mvgavg)

          #create a row index to allow merge with raw data
          temp_df <- temp_df %>%
            mutate(index = row_number())

          #define new column name based on variable
          new_colname <- paste0("mvg_avg_", i)

          #rename the column so that it's unique
          colnames(temp_df)[1]<- new_colname

          #merge temp_df and rawData dataframe
          main_DF <- merge(rawData, temp_df, by.x = "index", by.y = "index")

        }

修復問題中的輸入數據,我們將輸入cbind到它的滾動平均值。

library(zoo)
rawData <- data.frame(a = 101:465, b = 1001:1365, c = 1:365) # test data

rawData2 <- cbind(rawData, roll = rollmeanr(rawData, 7, fill = NA))

給予:

     a    b  c roll.a roll.b roll.c
1  101 1001  1     NA     NA     NA
2  102 1002  2     NA     NA     NA
3  103 1003  3     NA     NA     NA
4  104 1004  4     NA     NA     NA
5  105 1005  5     NA     NA     NA
6  106 1006  6     NA     NA     NA
7  107 1007  7    104   1004      4
8  108 1008  8    105   1005      5
9  109 1009  9    106   1006      6
10 110 1010 10    107   1007      7
11 111 1011 11    108   1008      8
12 112 1012 12    109   1009      9
... etc ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM