2017-03-23 5 views
0

誰かがタイトルに関するより良い提案をしている場合は、私に知らせてください。現在の列がRの2つ前の列と同じフィールドを共有する場合のみ、計算列を作成できますか?

私は以下のようなデータセットを持っていますが、現在の四半期の値2に現在の前の2/4の値1を追加する必要があります。前の四半期が見つからなかった場合は、値を入力しないでください。

データセット:

Dimension, Date, Value1, Value2 
1, 7/31/2001, 10, 20 
1, 10/31/2001, 10, 30 
1, 1/31/2002, 10, 40 
1, 4/30/2002, 20, 20 
1, 7/31/2003, 20, 40 
2, 12/31/2002, 30, 20 
2, 3/31/2003, 10, 20 
2, 6/30/2003, 20, 20 
2, 9/30/2003, 40, 10 

所望の出力:

Dimension, Date, Value1, Value2, Goal 
1, 7/31/2001, 10, 20, 50 
1, 10/31/2001, 10, 30, 70 
1, 1/31/2002, 10, 40, 90 
1, 4/30/2002, 20, 20, NA 
1, 7/31/2003, 20, 40, NA 
2, 12/31/2002, 30, 20, 80 
2, 3/31/2003, 10, 20, 80 
2, 6/30/2003, 20, 20, 90 
2, 9/30/2003, 40, 10, NA 

dputバージョン

入力

structure(list(Dimension = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L), Date = c("7/31/2001", "10/31/2001", "1/31/2002", "4/30/2002", 
"7/31/2003", "12/31/2002", "3/31/2003", "6/30/2003", "9/30/2003" 
), Value1 = c(10L, 10L, 10L, 20L, 20L, 30L, 10L, 20L, 40L), Value2 = c(20L, 
30L, 40L, 20L, 40L, 20L, 20L, 20L, 10L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -9L), .Names = c("Dimension", 
"Date", "Value1", "Value2"), spec = structure(list(cols = structure(list(
    Dimension = structure(list(), class = c("collector_integer", 
    "collector")), Date = structure(list(), class = c("collector_character", 
    "collector")), Value1 = structure(list(), class = c("collector_integer", 
    "collector")), Value2 = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("Dimension", "Date", "Value1", 
"Value2")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec")) 

出力

だから、
structure(list(Dimension = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L), Date = c("7/31/2001", "10/31/2001", "1/31/2002", "4/30/2002", 
"7/31/2003", "12/31/2002", "3/31/2003", "6/30/2003", "9/30/2003" 
), Value1 = c(10L, 10L, 10L, 20L, 20L, 30L, 10L, 20L, 40L), Value2 = c(20L, 
30L, 40L, 20L, 40L, 20L, 20L, 20L, 10L), Goal = c(50L, 70L, 90L, 
NA, NA, 80L, 80L, 90L, NA)), class = c("tbl_df", "tbl", "data.frame" 
), row.names = c(NA, -9L), .Names = c("Dimension", "Date", "Value1", 
"Value2", "Goal"), spec = structure(list(cols = structure(list(
    Dimension = structure(list(), class = c("collector_integer", 
    "collector")), Date = structure(list(), class = c("collector_character", 
    "collector")), Value1 = structure(list(), class = c("collector_integer", 
    "collector")), Value2 = structure(list(), class = c("collector_integer", 
    "collector")), Goal = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("Dimension", "Date", "Value1", 
"Value2", "Goal")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec")) 
+1

データセットに 'dput'の出力を追加できますか? – Marcelo

+0

dputバージョンを含むように質問を更新しました – Terry

+0

四半期のデータが連続しているか、データが四半期にスキップする可能性がありますか? – bouncyball

答えて

1

、あなたは「現在と 4分の2のために値1を追加します」と四半期は、我々がdplyrlubridateパッケージを使用することができ、データに連続して表示されることを意味と仮定。

まず、YearQtrの列を作成し、これらの列で並べ替えることができます。次に、group_byDimensionです。最後に、lead関数を使用して、次の2つの値、すなわちValueを取得します。

library(dplyr);library(lubridate) 

dat %>% 
    mutate(Date = mdy(Date), 
      Year = year(Date), 
      Qtr = quarter(Date)) %>% 
    arrange(Year, Qtr) %>% 
    group_by(Dimension) %>% 
    mutate(Goal = Value2 + Value1 + lead(Value1) + lead(Value1, 2)) %>% 
    select(-Year, -Qtr) 

    Dimension  Date Value1 Value2 Goal 
     <int>  <date> <int> <int> <int> 
1   1 2001-07-31  10  20 50 
2   1 2001-10-31  10  30 70 
3   1 2002-01-31  10  40 90 
4   1 2002-04-30  20  20 NA 
5   2 2002-12-31  30  20 80 
6   2 2003-03-31  10  20 90 
7   2 2003-06-30  20  20 NA 
8   1 2003-07-31  20  40 NA 
9   2 2003-09-30  40  10 NA 
関連する問題