【问题标题】:I have three lists with a vector of values, how can I put all three list's values at the same index into an existing dataframe?我有三个带有值向量的列表,如何将所有三个列表的值放在同一个索引中到现有数据框中?
【发布时间】:2021-09-11 23:23:49
【问题描述】:

一个完全可重现的例子。现在的问题是创建的新列在所有值的 c() 中,而不是每个值都在其单独的行中。

date = seq(as.Date("2019/01/01"), by = "month", length.out = 48)

productB = rep("B",48)
productB = rep("B",48)
productA = rep("A",48)
productA = rep("A",48)

subproducts1=rep("1",48)
subproducts2=rep("2",48)
subproductsx=rep("x",48)
subproductsy=rep("y",48)

b1 <- c(rnorm(30,5), rep(0,18))
b2 <- c(rnorm(30,5), rep(0,18))
b3 <-c(rnorm(30,5), rep(0,18))
b4 <- c(rnorm(30,5), rep(0,18))

在下面创建了数据框

dfone <- data.frame("date"= rep(date,4),
                "product"= c(rep(productB,2),rep(productA,2)),
                "subproduct"= 
c(subproducts1,subproducts2,subproductsx,subproductsy),
                "actuals"= c(b1,b2,b3,b4))

export_df <- split(dfone[1:4], dfone[3])

# Creation of data frames based off UNIQUE SUBPRODUCTS 
dummy_list <- split(dfone[1:4], dfone[3]) %>% lapply( function(x) 
x[(names(x) %in% c("date", "actuals"))])
dummy_list <-  lapply(dummy_list, function(x) { x["date"] <- NULL; x })


list_dfs <- list()
for (i in 1:length(unique(dfone$subproduct))) {
  #assign(paste0("df", i), as.data.frame(dummy_list[[i]]))
  list_dfs <-append(list_dfs,dummy_list[[i]])
}

combined_dfs <- Reduce(function(x, y) merge(x, y, all = TRUE,  
by='date'), list(list_dfs))

创建时间序列

list_ts <- lapply(list_dfs, function(t) 
ts(t,start=c(2019,1),end=c(2021,6), frequency = 12)) %>%
  lapply( function(t) ts_split(t,sample.out=(0.2*length(t))))    # 
creates my train test split
list_ts <- do.call("rbind", list_ts)  #Creates a list of time series

创建模型

model_tune_ses1 <- lapply(list_ts[1:(length(list_ts)/2)], function(x) 
forecast::forecast(ses(x,h=24,alpha=0.1))) 
model_tune_ses1 <- lapply(model_tune_ses1, "[",  c("mean"))

model_tune_ses2 <- lapply(list_ts[1:(length(list_ts)/2)], function(x) 
forecast::forecast(ses(x,h=24,alpha=0.2))) 
model_tune_ses2 <- lapply(model_tune_ses2, "[",  c("mean"))

model_trp_holt_mult <- lapply(list_ts[1:(length(list_ts)/2)], 
function(x)
  forecast::forecast( HoltWinters(x,seasonal="multiplicative"),h=24))

model_trp_holt_mult <- lapply(model_trp_holt_mult, "[",  c("mean"))
lst1 <- do.call(Map, c(f = cbind, mget(ls(pattern = 'model_'))))

export_df1 <- Map(cbind, export_df, lst1)


export_df1 <- bind_rows(export_df1, .id = "date")

我已删除其他编辑以节省更多空间。

编辑2:

export_df1[[6]]
[[1]]
      Jan      Feb      Mar      Apr      May      Jun      Jul      Aug      
Sep      Oct      Nov      Dec
2021 6.508872 4.639274 4.678671 4.626766 5.327353 6.890269 6.640483 
6.164311 5.317675 6.152747 5.963053 5.243159
2022 6.517052 4.645104 4.684550 4.632579 5.334046 6.898923 6.648823 
6.172052 5.324353 6.160472 5.970540 5.249740

    export_df1[[6]]
[[2]]
      Jan      Feb      Mar      Apr      May      Jun      Jul      Aug      
Sep      Oct      Nov      Dec
2021 6.508872 4.639274 4.678671 4.626766 5.327353 6.890269 6.640483 
6.164311 5.317675 6.152747 5.963053 5.243159
2022 6.517052 4.645104 4.684550 4.632579 5.334046 6.898923 6.648823 
6.172052 5.324353 6.160472 5.970540 5.249740

我不希望将数据框中的每一行都作为所有这些值的列表,而是希望列如下所示。

export_df1$actuals
  [1] 4.729682 4.573595 7.338069 4.742559 5.853501 3.167612 6.305137 
5.879437 4.571004 5.367115 5.127305 4.552428 5.843000 4.060205 4.125869 
4.190687
[17] 4.894595 6.454123 4.686262 4.196005 7.289879 6.206924 6.191610 

6.100175 4.769656 4.829249 5.287280 4.425435 5.158180 4.402173 0.000000 0.000000 等

【问题讨论】:

  • 我已经更新了帖子,是的,这是真的,所以这是我想在向量的输出匹配我的每个部分的长度 48 之前添加一个 0 的向量数据框。或者我们可以将 0 保留为 null 应该没问题,但它必须位于下半部分,而不是列的前半部分,例如 (0,0,0,0,........the从列的列表中输出)
  • 请查看mmy更新
  • 去测试我的真实代码看看会发生什么,但是在虚拟代码上这工作得很好。你是天才
  • 这非常有效。非常感谢你的帮助。想知道你是如何在 R 方面如此出色的,其中一些功能我以前从未听说过。有什么技巧/书籍可以阅读吗?
  • 我会说这就是你将如何学习 :=) 即艰难地学习。说真的,网上有很多书可以看,但在编程中,是靠自己的实践和调试来提高技能的。一开始可能听起来很难,但相信我,以后会更容易

标签: r dataframe data-manipulation


【解决方案1】:

我们可以在list 中获取所有“list_ts_ses”对象

lst1 <- do.call(Map, c(f = cbind, mget(ls(pattern = 'list_ts_ses'))))
export_df1 <- Map(cbind, export_df, lst1)

更新

out <- Map(function(x, y) {x[colnames(y)] <- sapply(y, 
    function(u) c(rep(0, nrow(x) - length(u)), u)); x}, export_df, lst1)

-检查结构

 str(out)
List of 4
 $ 1:'data.frame':  48 obs. of  7 variables:
  ..$ date               : Date[1:48], format: "2019-01-01" "2019-02-01" "2019-03-01" "2019-04-01" ...
  ..$ product            : chr [1:48] "B" "B" "B" "B" ...
  ..$ subproduct         : chr [1:48] "1" "1" "1" "1" ...
  ..$ actuals            : num [1:48] 5.57 5.16 5.33 4.15 4.29 ...
  ..$ model_trp_holt_mult: num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ model_tune_ses1    : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ model_tune_ses2    : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
 $ 2:'data.frame':  48 obs. of  7 variables:
  ..$ date               : Date[1:48], format: "2019-01-01" "2019-02-01" "2019-03-01" "2019-04-01" ...
  ..$ product            : chr [1:48] "B" "B" "B" "B" ...
  ..$ subproduct         : chr [1:48] "2" "2" "2" "2" ...
  ..$ actuals            : num [1:48] 6.5 5.07 4.06 6.9 3.72 ...
  ..$ model_trp_holt_mult: num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ model_tune_ses1    : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ model_tune_ses2    : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
 $ x:'data.frame':  48 obs. of  7 variables:
  ..$ date               : Date[1:48], format: "2019-01-01" "2019-02-01" "2019-03-01" "2019-04-01" ...
  ..$ product            : chr [1:48] "A" "A" "A" "A" ...
  ..$ subproduct         : chr [1:48] "x" "x" "x" "x" ...
  ..$ actuals            : num [1:48] 5.21 6.51 2.42 4.8 3.62 ...
  ..$ model_trp_holt_mult: num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ model_tune_ses1    : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ model_tune_ses2    : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
 $ y:'data.frame':  48 obs. of  7 variables:
  ..$ date               : Date[1:48], format: "2019-01-01" "2019-02-01" "2019-03-01" "2019-04-01" ...
  ..$ product            : chr [1:48] "A" "A" "A" "A" ...
  ..$ subproduct         : chr [1:48] "y" "y" "y" "y" ...
  ..$ actuals            : num [1:48] 5.01 5.39 5.23 5.43 3.99 ...
  ..$ model_trp_holt_mult: num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ model_tune_ses1    : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ model_tune_ses2    : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...

数据

export_df <- list(data.frame(col1 = 1:24, col2 = 25:48), data.frame(col1 = 1:24, col2 = 25:48), data.frame(col1 = 1:24, col2 = 25:48))
list_ts_ses <- list(ts(rnorm(24), frequency = 12, start = c(2021, 1)),ts(rnorm(24), frequency = 12, start = c(2021, 1)), ts(rnorm(24), frequency = 12, start = c(2021, 1)) )
list_ts_ses1 <- list(ts(rnorm(24), frequency = 12, start = c(2021, 1)),ts(rnorm(24), frequency = 12, start = c(2021, 1)), ts(rnorm(24), frequency = 12, start = c(2021, 1)) )
list_ts_ses2 <- list(ts(rnorm(24), frequency = 12, start = c(2021, 1)),ts(rnorm(24), frequency = 12, start = c(2021, 1)), ts(rnorm(24), frequency = 12, start = c(2021, 1)) )

【讨论】:

  • 很抱歉我之前没有看到 cmets,我一直在玩弄我的数据,并在上面进行了编辑。我知道在所有输出之前我需要一个包含 24 个向量的向量,例如 (c(rep(0,24), unlist(list_ts_ses[[4]])))。 list_ts_ses[1]等都需要进入export_df[1],反之亦然
  • 对不起,如果我不清楚。我有 x 个带有值向量的列表。我们可以说我们有 3 个列表。 Iist1[[3]]、list2[[3]]、list3[[3]]。我希望所有三个列表的索引为 3,作为新列添加到我的 export_df[[3]] 的数据框/小标题列表中。因此,从我在原始帖子中的 edit1 中,列应该是 tibble 48x 7。我的 list1[[3]] 的值只有 24,我需要在这些值之前添加一个 24 0 的向量,以便它匹配行长度48 在 export_df[[1]]
  • 我相信这几乎就是我想要的。 export_df1[[1]]$list_ts_ses,例如返回另一个 48 个值的列表,而不仅仅是一个长度为 48 的值的向量。我确实希望在我的 list[[1]] 输出之前有 24 个 0,因此它等于 48。
  • 我将使用您提供的示例数据,并明确我想要从输出中得到什么。今晚有空的时候我会更新你的。感谢您迄今为止的帮助。
  • 我玩过你的代码和我的代码,发现了问题,查看新的编辑 2.1 和编辑 3
猜你喜欢
  • 1970-01-01
  • 2019-11-13
  • 2021-05-26
  • 2018-07-19
  • 2015-04-18
  • 2020-05-29
  • 2017-02-18
  • 1970-01-01
  • 2020-05-07
相关资源
最近更新 更多