R 将数据帧与列的不同需求结合起来

R 将数据帧与列的不同需求结合起来,r,merge,R,Merge,有两个数据帧,如 这个: df1 <- data.frame(a_id = c(42,3234,1445,34), text = c("sth1","sthe2","sthe3","sther4"), product_id = c("Google","Yahoo","Amazon","Yahoo")) df2 <- data.frame(b_id = c(55,78,2345467,23,42312,44), text = c("st1","st

有两个数据帧,如 这个:

df1 <- data.frame(a_id = c(42,3234,1445,34),
text = c("sth1","sthe2","sthe3","sther4"),
product_id = c("Google","Yahoo","Amazon","Yahoo"))
df2 <- data.frame(b_id = c(55,78,2345467,23,42312,44),
                  text = c("st1","sth2","sth3","sth4","sth5","sth6"),
                  product_id = c("Yahoo","Google","Amazon","Amazon","Amazon","Google"))

我们可以使用
map
来执行此操作。将数据集放置在
列表中
,使用
映射
在数据集上循环,
通过
粘贴
更改列名前缀,并重命名列名,从而变异
第一列

library(tidyverse)
list(df1, df2) %>% 
     map_df(~ .x %>% 
                 mutate(!! names(.x)[1] := paste0(substr(names(.x)[1],
                  1, 1), !! rlang::sym(names(.x)[1]))) %>% 
                 rename_at(1, ~ "ab_id"))
#     ab_id   text product_id
#1       a42   sth1     Google
#2     a3234  sthe2      Yahoo
#3     a1445  sthe3     Amazon
#4       a34 sther4      Yahoo
#5       b55    st1      Yahoo
#6       b78   sth2     Google
#7  b2345467   sth3     Amazon
#8       b23   sth4     Amazon
#9    b42312   sth5     Amazon
#10      b44   sth6     Google
上述内容也可以封装在函数中

fbind <- function(dat1, dat2) {
           list(dat1, dat2) %>%
               map_df( ~ 
                       .x %>%
                        mutate(!! names(.x)[1] := paste0(substr(names(.x)[1],
                  1, 1), !! rlang::sym(names(.x)[1]))) %>% 
                 rename_at(1, ~ "ab_id"))
     }

fbind(df1, df2)
#     ab_id   text product_id
#1       a42   sth1     Google
#2     a3234  sthe2      Yahoo
#3     a1445  sthe3     Amazon
#4       a34 sther4      Yahoo
#5       b55    st1      Yahoo
#6       b78   sth2     Google
#7  b2345467   sth3     Amazon
#8       b23   sth4     Amazon
#9    b42312   sth5     Amazon
#10      b44   sth6     Google
fbind%
地图(df)
.x%>%
mutate(!!names(.x)[1]:=paste0(substr(names(.x)[1],
1,1),!!rlang::sym(名称(.x)[1]))%>%
在(1,~“ab\u id”)处重命名
}
fbind(df1,df2)
#ab_id文本产品_id
#1 a42 sth1谷歌
#2 a3234和2雅虎
#3 A14453亚马逊
#4 A34sTher4雅虎
#5 b55 st1雅虎
#6 b78 sth2谷歌
#7 b2345467 sth3亚马逊
#8 b23 sth4亚马逊
#9 b42312 sth5亚马逊
#10 b44 sth6谷歌

我们可以使用
map
来执行此操作。将数据集放置在
列表中
,使用
映射
在数据集上循环,
通过
粘贴
更改列名前缀,并重命名列名,从而变异
第一列

library(tidyverse)
list(df1, df2) %>% 
     map_df(~ .x %>% 
                 mutate(!! names(.x)[1] := paste0(substr(names(.x)[1],
                  1, 1), !! rlang::sym(names(.x)[1]))) %>% 
                 rename_at(1, ~ "ab_id"))
#     ab_id   text product_id
#1       a42   sth1     Google
#2     a3234  sthe2      Yahoo
#3     a1445  sthe3     Amazon
#4       a34 sther4      Yahoo
#5       b55    st1      Yahoo
#6       b78   sth2     Google
#7  b2345467   sth3     Amazon
#8       b23   sth4     Amazon
#9    b42312   sth5     Amazon
#10      b44   sth6     Google
上述内容也可以封装在函数中

fbind <- function(dat1, dat2) {
           list(dat1, dat2) %>%
               map_df( ~ 
                       .x %>%
                        mutate(!! names(.x)[1] := paste0(substr(names(.x)[1],
                  1, 1), !! rlang::sym(names(.x)[1]))) %>% 
                 rename_at(1, ~ "ab_id"))
     }

fbind(df1, df2)
#     ab_id   text product_id
#1       a42   sth1     Google
#2     a3234  sthe2      Yahoo
#3     a1445  sthe3     Amazon
#4       a34 sther4      Yahoo
#5       b55    st1      Yahoo
#6       b78   sth2     Google
#7  b2345467   sth3     Amazon
#8       b23   sth4     Amazon
#9    b42312   sth5     Amazon
#10      b44   sth6     Google
fbind%
地图(df)
.x%>%
mutate(!!names(.x)[1]:=paste0(substr(names(.x)[1],
1,1),!!rlang::sym(名称(.x)[1]))%>%
在(1,~“ab\u id”)处重命名
}
fbind(df1,df2)
#ab_id文本产品_id
#1 a42 sth1谷歌
#2 a3234和2雅虎
#3 A14453亚马逊
#4 A34sTher4雅虎
#5 b55 st1雅虎
#6 b78 sth2谷歌
#7 b2345467 sth3亚马逊
#8 b23 sth4亚马逊
#9 b42312 sth5亚马逊
#10 b44 sth6谷歌

您可以在函数中执行此操作

myMerge <- function(x, y) {
  nm <- names(x)[-1]
  names(x) <- names(y) <- 1:3
  x[, 1] <- paste0("a", x[, 1])
  y[, 1] <- paste0("b", y[, 1])
  return(setNames(rbind(x, y), c("ab_id", nm)))
}

您可以在函数中执行此操作

myMerge <- function(x, y) {
  nm <- names(x)[-1]
  names(x) <- names(y) <- 1:3
  x[, 1] <- paste0("a", x[, 1])
  y[, 1] <- paste0("b", y[, 1])
  return(setNames(rbind(x, y), c("ab_id", nm)))
}