从 R 中的数据框向数据框列表中的特定行添加列

Add columns to specific rows in a list of data frames from a data frame in R

我想在下面提到的列表的 headers 旁边添加两列(第一个数据框除外)。

数据框列表的结构multidf 我想在其中添加列

structure(list(`0` = structure(list(X01.04.2020 = 0:1, Asset = c("PORTFOLIO", "CASH"), Position = c("--", "--"), Price = c("--", "--"), Mark.to.Market = c(1000000L, 1000000L)), class = "data.frame", row.names = c(NA, -2L)), `1` = structure(list(X02.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "500", "-5931", "Buys:"), Price = c("--", "--", "2516", "47", "1"), Mark.to.Market = c("999231", "509866", "1258250", "-279795", "Sells:"), Position.prior = c(NA, NA, 0L, 0L, 1L), Transaction = c("", "", "500", "-5931", "TC spent:"), TC.spent = c(NA, NA, 629L, 140L, 769L)), class = "data.frame", row.names = c(NA, -5L)), `2` = structure(list(X03.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "505", "-6206", "Buys:"), Price = c("--", "--", "2483", "45", "1"), Mark.to.Market = c("995708", "508078", "1253789", "-278805", "Sells:"), Position.prior = c(NA, NA, 500L, -5931L, 1L), Transaction = c("", "", "5", "-275", "TC spent:"), TC.spent = c(NA, NA, 6L, 6L, 12L)), class = "data.frame", row.names = c(NA, -5L)), `3` = structure(list(X06.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "522", "-7352", "Buys:"), Price = c("--", "--", "2644", "42", "1"), Mark.to.Market = c("1096915", "559989", "1380429", "-307130", "Sells:"), Position.prior = c(NA, NA, 505L, -6206L, 1L), Transaction = c("", "", "17", "-1146", "TC spent:"), TC.spent = c(NA, NA, 22L, 24L, 46L)), class = "data.frame", row.names = c(NA, -5L)), `4` = structure(list(X07.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "514", "-6863", "Buys:"), Price = c("--", "--", "2642", "44", "1"), Mark.to.Market = c("1079069", "550870", "1357988", "-302144", "Sells:"), Position.prior = c(NA, NA, 522L, -7352L, 1L), Transaction = c("", "", "-8", "489", "TC spent:"), TC.spent = c(NA, NA, 11L, 11L, 21L)), class = "data.frame", row.names = c(NA, -5L)), `5` = structure(list(X08.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "524", "-7521", "Buys:"), Price = c("--", "--", "2735", "42", "1"), Mark.to.Market = c("1138189", "580800", "1433140", "-318702", "Sells:"), Position.prior = c(NA, NA, 514L, -6863L, 1L), Transaction = c("", "", "10", "-658", "TC spent:"), TC.spent = c(NA, NA, 14L, 14L, 28L)), class = "data.frame", row.names = c(NA, -5L))), .Dim = 6L, .Dimnames = list(`cumsum(!grepl("\S", txt))` = c("0", "1", "2", "3", "4", "5")))

这是预期的结果

数据框的结构df3(可重现示例的最后两列)需要添加到数据框列表中,如上所示

structure(c("DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "FLAT", "UP", "UP", "UP", "UP", "UP", "UP"), .Dim = c(6L, 2L))

我使用以下代码获取数据帧列表multidf和数据帧df3

#Load the two input files in a data frame
df1 <- read.csv("SP500Preds_20210202.csv")
df2 <- read.csv("VIXPredPerfectForesight_20210213.csv")
#Extract and combine the required items to be loaded in final output
df3 <- cbind(df1[, c(3)], df2[, c(3)])
#Load the file that needs to be updated
txt <- readLines("ProcessedTrades.csv")
multidf <- by(txt, cumsum(!grepl("\S", txt)), 
  FUN = function(x) read.csv(text = x, strip.white = TRUE))

为了得到预期的结果,我使用了下面的代码

multidf[-1] <- Map(cbind, multidf[-1], asplit(df3, 1))

我收到以下错误

multidf[-1] <- Map(cbind, multidf[-1], asplit(df3, 1)) Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 5, 2

谁能帮我解决这个问题? 提前致谢。

在下方添加可重现的 CSV 文件示例

SP500Preds_20210202.csv 或 df1

structure(list(date = c("04/06/19", "05/06/19", "06/06/19", "07/06/19", "10/06/19", "11/06/19"), prediction_for_date = c("11/6/2019", "12/6/2019", "13/06/19", "14/06/19", "17/06/19", "18/06/19"), Predictions = c("DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "FLAT")), row.names = c(NA, 6L), class = "data.frame")

VIXPredPerfectForesight_20210213.csv 或 df2

structure(list(date = c(20190604L, 20190605L, 20190606L, 20190607L, 20190610L, 20190611L), X1.Day.Pred = c("DOWN", "DOWN", "UP", "DOWN", "UP", "DOWN"), X5.Day.Pred = c("UP", "UP", "UP", "UP", "UP", "UP")), row.names = c(NA, 6L), class = "data.frame")  

list multidf 中有 6 个元素 (data.frame),matrix 'df3' 中有 6 行。因此,如果打算在每个 list 元素中创建两个新列

multidf2 <- Map(cbind, multidf, lapply(asplit(df3, 1), as.list))

apply

multidf2 <- Map(cbind, multidf, apply(df3, 1, as.list))

如果我们只需要创建两个空白列,列名如 'df3' 行

multidf2 <-  Map(function(x, y) {x[y] <- ''; x}, multidf, asplit(df3, 1))