合并两个不同列名的df,然后melt

Combine two dfs with different column names and then melt

我想合并两个数据框,但根据以下内容将它们融合到不同的列中:

treatment<-c('control','noise')
weight<-c(0.01872556,0.01575400)
sd<-c(0.008540041,0.007460524)

df1<-data.frame(treatment,weight,sd)

treatment2<-c('control','noise')
area<-c(0.79809444,0.68014667)
sd2<-c(0.337949414,0.294295847)

df2<-data.frame(treatment2,area,sd2)

我想将它们组合起来并创建一个如下所示的数据框:

treatment var sum sd
control area 0.79809444 0.337949414
noise area 0.68014667 0.294295847
control weight 0.01872556 0.008540041
noise weight 0.01575400 0.01575400

我尝试了各种方法,用谷歌搜索了各种方法,最终将每个数据帧导出到一个 csv 文件中,然后将它们合并到 excel 中,然后重新导入到 R 中进行分析。

有没有更简单的解决方案?

您可以使用 {purrr} 和 {dplyr} 中的函数执行此操作:

map(list(df2, df1), ~ mutate(., var = colnames(.)[2])) %>%
  map(~ set_names(., nm = c("treatment", "sum", "sd", "var"))) %>%
  bind_rows() %>%
  relocate("var", .before = "sum")

输出:

  treatment    var        sum          sd
1   control   area 0.79809444 0.337949414
2     noise   area 0.68014667 0.294295847
3   control weight 0.01872556 0.008540041
4     noise weight 0.01575400 0.007460524

这是一个 dplyr 解决方案。策略是先把两个dfs处理成想要的格式再合并

df1 <- df1 %>% 
  dplyr::mutate(var = "weight") %>% 
  dplyr::rename(sum = weight)
df2 <- df2 %>% 
  dplyr::mutate(var = "area") %>% 
  dplyr::rename(treatment = treatment2,
                sd = sd2,
                sum = area)

dplyr::bind_rows(df1, df2)

# output
  treatment        sum          sd    var
1   control 0.01872556 0.008540041 weight
2     noise 0.01575400 0.007460524 weight
3   control 0.79809444 0.337949414   area
4     noise 0.68014667 0.294295847   area

你可以使用

library(tidyr)
library(dplyr)

df2 %>% 
  rename(sd = sd2, treatment = treatment2) %>% 
  pivot_longer(area, names_to = "var", values_to = "sum") %>% 
  bind_rows(pivot_longer(df1, weight, names_to = "var", values_to = "sum")) %>% 
  select(treatment, var, sum, sd)

获得

# A tibble: 4 x 4
  treatment var       sum      sd
  <chr>     <chr>   <dbl>   <dbl>
1 control   area   0.798  0.338  
2 noise     area   0.680  0.294  
3 control   weight 0.0187 0.00854
4 noise     weight 0.0158 0.00746