2017-10-20 131 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”)'? –

相关问题