![](/img/trans.png)
[英]extracting particular column from a list of multiple data frames and combine them into a new data frame in r
[英]Extracting the same column from multiple data frames and cbind them into a new data frame in R
我正在尋找一行代碼,它允許我從多個數據框中提取同名的列並將它們綁定到一個 dataframe 中。我還希望在 dataframe 之后命名新的 dataframe 中的每一列來自。
下面是我一直在使用可重現數據的代碼。 我一直在嘗試 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))
一個基於 tidyverse 的想法在被問到這個問題時可能不存在,但應該可以很好地擴展。 唯一需要指定數據集名稱的地方是將數據框捆綁到一個列表中; 在返回數據幀列表的情況下(例如從目錄中讀取文件),您甚至不需要在那里進行。 tibble::lst
是基本list
的包裝器,它按原始 object 名稱命名值。 {{ ds }}:= Ending.Value
是一種簡潔的表示法,可根據變量ds
中的值動態重命名Ending.Value
列,在本例中為數據集的名稱。
library(dplyr)
library(xts)
tibble::lst(Asset, Register, Ledger) %>%
purrr::map(as.data.frame) %>%
purrr::imap(function(dat, ds) select(dat, {{ ds }} := Ending.Value)) %>%
bind_cols()
#> Asset Register Ledger
#> 2019-02-19 63.39797 212.1563 160.6881
#> 2019-02-20 63.67143 213.7188 161.7900
#> 2019-02-21 62.95599 211.6355 160.4143
#> 2019-02-22 63.00784 211.8091 160.6648
#> 2019-02-25 62.88932 211.6355 160.7789
#> 2019-02-26 62.09857 207.9896 160.0476
如果您不介意在列表中命名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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.