我在这里遇到问题,希望你们能帮忙。 我有以下格式的数据(见图片):
structure(list(Activity = 1:4, Date1 = c("01/09/2019", "01/09/2019",
"01/09/2019", "01/09/2019"), Amount1 = c(10L, 20L, 30L, 40L),
Date2 = c("01/10/2019", "01/10/2019", "01/10/2019", NA),
Amount2 = c(32L, 34L, 12L, NA), Date3 = c("01/11/2019", "01/11/2019",
"01/11/2019", NA), Amount3 = c(54L, 12L, 43L, NA), Date4 = c("01/12/2019",
NA, "01/12/2019", NA), Amount4 = c(56L, NA, 124L, NA)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
cols = list(Activity = structure(list(), class = c("collector_integer",
"collector")), Date1 = structure(list(), class = c("collector_character",
"collector")), Amount1 = structure(list(), class = c("collector_integer",
"collector")), Date2 = structure(list(), class = c("collector_character",
"collector")), Amount2 = structure(list(), class = c("collector_integer",
"collector")), Date3 = structure(list(), class = c("collector_character",
"collector")), Amount3 = structure(list(), class = c("collector_integer",
"collector")), Date4 = structure(list(), class = c("collector_character",
"collector")), Amount4 = structure(list(), class = c("collector_integer",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
可以有数百个活动,所有这些活动的日期和金额都可以有0、1、2、3或4个条目。我需要以某种方式将其“转置”为这种格式:
我的想法是,可以通过for循环中的嵌套while循环解决此问题,但是我不太熟练R,因此感到有点迷茫。
一些注意事项:
如果您需要上述说明,请告诉我-希望您能在这里为我提供帮助! :)
/雅各布