Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数将数据行与R(和tidyverse)中的附加源列连接起来_R_Concatenation_Tibble - Fatal编程技术网

函数将数据行与R(和tidyverse)中的附加源列连接起来

函数将数据行与R(和tidyverse)中的附加源列连接起来,r,concatenation,tibble,R,Concatenation,Tibble,我希望能够获取格式相同但来源不同的数据,并连接行,但为了跟踪数据的来源,我还想引入一个源列 这看起来很常规,我想我应该创建一个实用函数来完成它,但我在让它工作时遇到了麻烦 以下是我尝试过的: library(tidyverse) tibble1 = tribble( ~a, ~b, 1,2, 3,4 ) tibble2 = tribble( ~a, ~b, 5,6 ) bind_rows_with_source <- function(...){ out =

我希望能够获取格式相同但来源不同的数据,并连接行,但为了跟踪数据的来源,我还想引入一个源列

这看起来很常规,我想我应该创建一个实用函数来完成它,但我在让它工作时遇到了麻烦

以下是我尝试过的:

library(tidyverse)

tibble1 = tribble(
  ~a, ~b,
  1,2,
  3,4
)

tibble2 = tribble(
  ~a, ~b,
  5,6
)

bind_rows_with_source <- function(...){
  out = tibble()
  for (newtibb in list(...)){
    out <- bind_rows(out, newtibb %>% mutate(source = deparse(substitute(newtibb))))
  }
  return(out)
}

bind_rows_with_source(tibble1,tibble2)
#source column just contains the string 'newtibb' on all rows
#I want it to contain tibble1 for the first two rows and tibble2 for the third:
#~a, ~b, ~source
# 1,  2, tibble1
# 3,  4, tibble1
# 5,  6, tibble2
库(tidyverse)
tibble1=tribble(
~a,~b,
1,2,
3,4
)
tibble2=tribble(
~a,~b,
5,6
)
将\u行\u与\u源绑定这可以通过以下方式完成:

bind_rows(list(tibble1=tibble1, tibble2=tibble2), .id='source')
# A tibble: 3 x 3
  source      a     b
  <chr>   <dbl> <dbl>
1 tibble1     1     2
2 tibble1     3     4
3 tibble2     5     6
bind_行(列表(tibble1=tibble1,tibble2=tibble2),.id='source')
#一个tibble:3x3
来源a b
1藏书11 2
2台1 3 4
3藏书2 5 6

如果您指的是不输入名称:

bind_rows_with_source <- function(..., .id = 'source'){
  bind_rows(setNames(list(...), as.character(substitute(...()))), .id = .id)
}

bind_rows_with_source(tibble1,tibble2)
# A tibble: 3 x 3
  source      a     b
  <chr>   <dbl> <dbl>
1 tibble1     1     2
2 tibble1     3     4
3 tibble2     5     6

bind_rows\u with_source如果您确实希望函数签名与
一起使用,您可以使用

bind_rows_with_source <- function(...){
  tibbleNames <- as.character(unlist(as.list(match.call())[-1]))
  tibbleList <- setNames(lapply(tibbleNames,get),tibbleNames)
  sourceCol <- rep(tibbleNames,times=sapply(tibbleList,NROW))
  out <- do.call("rbind",tibbleList)
  out$source <- sourceCol
  return(out)
}

我们可以使用
lazyeval
package:使用公式进行非标准评估的替代方法。提供LISP风格的“准旋转”的完整实现,使使用其他代码生成代码变得更容易。

库(lazyeval)

my_函数另一个选项是
rbindlist

library(data.table)
rbindlist(list(tibble1, tibble2), idcol = 'source')

杰出的我只是没有意识到bind_行具有.id功能。与列表方法相结合的正是我所需要的。@ LogMulnBuLs,如果这回答了你的问题,你可以考虑接受答案。这看起来是一个非常好的整洁的方法,以避免将列表作为输入输入到BIDIX行。(我猜dplyr代码的最后一行是要使用TIBLE列表,而不是键入列表?)是的,完全正确。修好了!
library(lazyeval)
my_function <- function(df) {
  df <- df %>% mutate(ref = expr_label(df))
  return(df)
}

a <- my_function(tibble1)
b <- my_function(tibble2)
bind_rows(a, b)
      a     b ref      
  <dbl> <dbl> <chr>    
1     1     2 `tibble1`
2     3     4 `tibble1`
3     5     6 `tibble2`
library(data.table)
rbindlist(list(tibble1, tibble2), idcol = 'source')