2017-10-20 130 views
0

我已經在我的環境無數dataframes:行綁定多個dataframes並添加起始數據幀的名字變成

x1 <- structure(list(time = structure(c(1327241343, 1327327803, 1327414263 
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), x1 = c(22.5, 
12, 0)), .Names = c("time", "x1"), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -3L)) 

x2 <- structure(list(time = structure(c(1326636543, 1326636603, 1326636663 
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), x2 = c(8, 
6, 1)), .Names = c("time", "x2"), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -3L)) 

x3 <- structure(list(time = structure(numeric(0), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), x3 = numeric(0)), .Names = c("time", 
"x1"), class = c("tbl_df", "tbl", "data.frame"), row.names = integer(0)) 

##----------------------------------------------------------------------------- 
## PREVIEW 
##----------------------------------------------------------------------------- 
> knitr::kable(x1) 

|time    | x1| 
|:-------------------|----:| 
|2012-01-22 14:09:03 | 22.5| 
|2012-01-23 14:10:03 | 12.0| 
|2012-01-24 14:11:03 | 0.0| 

> knitr::kable(x2) 

|time    | x2| 
|:-------------------|--:| 
|2012-01-15 14:09:03 | 8| 
|2012-01-15 14:10:03 | 6| 
|2012-01-15 14:11:03 | 1| 

> knitr::kable(x3) 

|time | x1| 
|:----|--:| 

注意,X3是一個空的數據幀,因爲這反映了我的情況。我想獲得以下行綁定的單數據幀

x.all <- structure(list(time = structure(c(1327241343, 1327327803, 1327414263, 
1326636543, 1326636603, 1326636663), class = c("POSIXct", "POSIXt" 
), tzone = "UTC"), x = c(22.5, 12, 0, 8, 6, 1), which = c("x1", 
"x1", "x1", "x2", "x2", "x2")), .Names = c("time", "x", "which" 
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L)) 

##----------------------------------------------------------------------------- 
## PREVIEW 
##----------------------------------------------------------------------------- 
> knitr::kable(x.all) 

|time    | x|which | 
|:-------------------|----:|:-----| 
|2012-01-22 14:09:03 | 22.5|x1 | 
|2012-01-23 14:10:03 | 12.0|x1 | 
|2012-01-24 14:11:03 | 0.0|x1 | 
|2012-01-15 14:09:03 | 8.0|x2 | 
|2012-01-15 14:10:03 | 6.0|x2 | 
|2012-01-15 14:11:03 | 1.0|x2 | 

我知道如何通過一個做到這一點的。但是,有超過100個數據框,我正在尋找一種有效的方法(每個數據框包含2列和> 500,000行)。

謝謝。

回答

1

這應該適用於你,使用tidyverse。使用get按名稱獲取數據。

listofdf <- paste0("x", 1:3) 
# "x1" "x2" "x3" 

library(tidyverse) 
map_df(listofdf, ~get(.x) %>% setNames(c("time","x")), .id="which") %>% 
    mutate(which = paste0("x", which)) %>% 
    select(time, x, which) 

# # A tibble: 6 x 3 
       # time  x which 
       # <dttm> <dbl> <chr> 
# 1 2012-01-22 14:09:03 22.5 x1 
# 2 2012-01-23 14:10:03 12.0 x1 
# 3 2012-01-24 14:11:03 0.0 x1 
# 4 2012-01-15 14:09:03 8.0 x2 
# 5 2012-01-15 14:10:03 6.0 x2 
# 6 2012-01-15 14:11:03 1.0 x2 

編輯工作與數據並不總是相同的模式

你需要做2次修改

開始獲取你的[R環境ls()

listofdf <- ls() 
# "x1" "x2" "x3" 

數據獲取ID爲mutate(which = listofdf[as.integer(which)])而不是mutate(which = paste0("x", which))

map_df(listofdf, ~get(.x) %>% setNames(c("time","x")), .id="which") %>% 
    mutate(which = listofdf[as.integer(which)]) %>% # 2nd change 
    select(time, x, which) 
+0

謝謝。這幾乎奏效。你能編輯一個案例,當x變量不能從一個模式構造出來(即'paste0(「x」,1:3)')?很高興接受它作爲答案。 –

+0

'listofdf < - c(「x1」,「x2」,「x3」)'? –

相關問題