我希望能够获取格式相同但来自不同来源的数据,并将行连接起来,但是要跟踪数据的来源,我还想介绍一个源列。
这似乎是例行公事,所以我想我应该创建一个实用程序函数来完成它,但是我很难让它开始工作。
以下是我尝试过的:
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是否已经有能够实现这一目标的功能?有比我尝试创建的实用程序函数更好的方法吗?有办法纠正我的做法吗?
非常感谢你读了我的问题
发布于 2021-06-02 17:04:53
另一个选择是rbindlist
library(data.table)
rbindlist(list(tibble1, tibble2), idcol = 'source')https://stackoverflow.com/questions/67807951
复制相似问题