从多个数据框中提取同一列,并将它们绑定到R中的新数据框中

时间:2019-02-28 22:10:00

标签: r

我正在寻找一行代码,该代码允许我从多个数据框中提取相同名称的列并将其绑定到单个数据框中。我还希望在新数据框中的每一列以该数据帧来自的名称命名。

下面是我一直在使用可复制数据的代码。我一直在尝试do.call,但是我无法正常工作:

Asset   <- structure(c(63.281303433027, 63.3979720475464, 63.6714334032718, 
            62.9559893597375, 63.0078420773017, 62.8893215800121, 31.6989860237732, 
            31.8357167016359, 31.4779946798687, 31.5039210386508, 31.4446607900061, 
            31.0492838185792, 63.3979720475464, 63.6714334032718, 62.9559893597375, 
            63.0078420773017, 62.8893215800121, 62.0985676371584), 
            class = c("xts","zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", 
            index = structure(c(1550534400, 1550620800, 1550707200, 1550793600, 1551052800, 1551139200),tzone = "UTC", tclass = "Date"), .Dim = c(6L, 3L), 
            .Dimnames = list(NULL, c("Beginning.Value", "Unit.Price", "Ending.Value")))

Register<- structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 212.156319855224, 
            213.718845942538, 211.63547782612, 211.809091835821, 211.63547782612, 
            207.989583622389),
            class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", 
            index = structure(c(1550534400,1550620800, 1550707200, 1550793600, 1551052800, 1551139200), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 3L), 
            .Dimnames = list(NULL, c("Amount", "Taxes", "Ending.Value")))

Ledger<- structure(c(0.994402284972246, 1.00685740995534, 0.991497559782253, 
            1.00156143848816, 1.00071020618011, 0.995451606923588, 161.592601088027, 
            160.688051756542, 161.789955602362, 160.414346177021, 160.664823311196, 
            160.778928461638, 160.688051756542, 161.789955602362, 160.414346177021, 
            160.664823311196, 160.778928461638, 160.04764269659), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", 
            index = structure(c(1550534400, 1550620800, 1550707200, 1550793600, 1551052800, 1551139200), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 3L), 
            .Dimnames = list(NULL, c("Discount_Proxy", "Beginning.Value","Ending.Value")))

dfs <- data.frame(c("Ledger","Registry","Ledger"))
names(dfs) <- "Data Frame"

Values <- do.call('cbind', list(dfs[,1]$Ending.Value))

1 个答案:

答案 0 :(得分:1)

如果您不介意为列表中的data.frames命名:

list_ls <- list("Asset" = Asset, "Register" = Register, "Ledger" = Ledger)

foo <- do.call(cbind, lapply(list_ls, function(x) x$Ending.Value))

test <- cbind(Asset$Ending.Value, Register$Ending.Value, Ledger$Ending.Value)
colnames(test) <- c("Asset", "Register", "Ledger")

length(which(foo != test))