1

I want to combine two data frames but melt them into different columns based on below:

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)

And I wanted to combine them and create a data frame which should look like this:

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

I tried this various ways, googled various ways and ended up exporting each data frame into a csv then combining them in excel, re-importing into R for analysis.

Is there a simpler solution?

GIS_newbie
  • 35
  • 4
  • i am not sure what end result you want. Can you update the question with your expected output? thanks. also the object 'treatment' in df1 is not defined. – Jagge Aug 24 '21 at 15:03
  • 1
    Apologies - have updated the question with expected output and object treatment. Just to clarify also, the two data frame examples posted above I gained from another output - thus wanting to combine them. – GIS_newbie Aug 24 '21 at 15:29
  • great, much easier to help. – Jagge Aug 24 '21 at 17:54
  • thank you, all solutions worked!! – GIS_newbie Sep 15 '21 at 10:11

3 Answers3

1

You could use

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)

to get

# 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
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
0

You could do this using functions from {purrr} and {dplyr}:

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

Output:

  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
Rory S
  • 1,278
  • 5
  • 17
0

Here is a dplyr solution. The strategy is to first process the two dfs to the desired format before merging them

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
Jagge
  • 938
  • 4
  • 21