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"))
データセットに 'dput'の出力を追加できますか? – Marcelo
dputバージョンを含むように質問を更新しました – Terry
四半期のデータが連続しているか、データが四半期にスキップする可能性がありますか? – bouncyball