2

I have two data-frames

The first data

        col1  col2 col3
A1       4     11  15
A2       2      9  17
A3       3      4   4 
B1       10     5   4   
B2        6     1   8
C1       12     1  12
C2        2     5   8
D1        4     1   6 
D2        2     1   8

The second data

          meancol1   meancol2 meancol3
meanA        3          8      12
meanB        8          3       6
meanC        7          3      10
meanD        3          1       7

I want to combine the two data-frames and keep the colnames of the first dataset so the result i want is :

          col1  col2 col3
    A1       4     11  15
    A2       2      9  17
    A3       3      4   4 
    B1       10     5   4   
    B2        6     1   8
    C1       12     1  12
    C2        2     5   8
    D1        4     1   6 
    D2        2     1   8
 meanA        3     8   12
 meanB        8     3    6
 meanC        7     3   10
 meanD        3     1    7

I tried : the following function

data_all <- rbind(df1,df2)

but it didn't work

I also tried the function bind_rows from dplyr package but this one create new columns.

Thank you

1
  • Start with names(df2) <- sub("mean", "", names(df2)) and then rbind the df's. May 4, 2022 at 13:30

5 Answers 5

4

You could always do:

colnames(df2) <- colnames(df1)

data_all <- rbind(df1, df2)

data_all
1
  • Thank you for you answer , it works , but i'm i want to knwo if there's any way to do this in one command,
    – Reda
    May 4, 2022 at 13:32
3

A possible solution:

library(tidyverse)

df1 <- read.table(text = "        col1  col2 col3
A1       4     11  15
A2       2      9  17
A3       3      4   4 
B1       10     5   4   
B2        6     1   8
C1       12     1  12
C2        2     5   8
D1        4     1   6 
D2        2     1   8
", header=T)

df2 <- read.table(text = "meancol1   meancol2 meancol3
meanA        3          8      12
meanB        8          3       6
meanC        7          3      10
meanD        3          1       7
", header=T)

df2 %>% rename_with(~ str_remove(.x, "mean")) %>% 
  bind_rows(df1, .)

#>       col1 col2 col3
#> A1       4   11   15
#> A2       2    9   17
#> A3       3    4    4
#> B1      10    5    4
#> B2       6    1    8
#> C1      12    1   12
#> C2       2    5    8
#> D1       4    1    6
#> D2       2    1    8
#> meanA    3    8   12
#> meanB    8    3    6
#> meanC    7    3   10
#> meanD    3    1    7
2
  • Notice that the two dataframes have to have the same column names in order to have bind_rows working fine, @Scienceslover. That is what my solution does.
    – PaulS
    May 4, 2022 at 13:40
  • 1
    Yes, either way i have to select variables right for example if my first data have two other columns i should do df2[,1:3] %>% rename_with(~ str_remove(.x, "mean")) %>% bind_rows(df1, .) right ?
    – Reda
    May 4, 2022 at 13:46
2

You could do:

rbind(df1, setNames(df2, names(df1)))

      col1 col2 col3
A1       4   11   15
A2       2    9   17
A3       3    4    4
B1      10    5    4
B2       6    1    8
C1      12    1   12
C2       2    5    8
D1       4    1    6
D2       2    1    8
meanA    3    8   12
meanB    8    3    6
meanC    7    3   10
meanD    3    1    7
0

Use mapply:

data.frame(mapply(c, df1, df2))

output

      id col1 col2 col3
1     A1    4   11   15
2     A2    2    9   17
3     A3    3    4    4
4     B1   10    5    4
5     B2    6    1    8
6     C1   12    1   12
7     C2    2    5    8
8     D1    4    1    6
9     D2    2    1    8
10 meanA    3    8   12
11 meanB    8    3    6
12 meanC    7    3   10
13 meanD    3    1    7

data

df1 <- read.table(text = "id        col1  col2 col3
A1       4     11  15
A2       2      9  17
A3       3      4   4 
B1       10     5   4   
B2        6     1   8
C1       12     1  12
C2        2     5   8
D1        4     1   6 
D2        2     1   8
", header=T)

df2 <- read.table(text = "id meancol1   meancol2 meancol3
meanA        3          8      12
meanB        8          3       6
meanC        7          3      10
meanD        3          1       7
", header=T)
0

Another option is to use rbindlist:

library(data.table)

rbindlist(lapply(list(df1, df2), setDT, keep.rownames = TRUE))

# Or using `add_rownames` from `dplyr`
# rbindlist(lapply(list(df1, df2), add_rownames), use.names = F)

Output

       rn col1 col2 col3
 1:    A1    4   11   15
 2:    A2    2    9   17
 3:    A3    3    4    4
 4:    B1   10    5    4
 5:    B2    6    1    8
 6:    C1   12    1   12
 7:    C2    2    5    8
 8:    D1    4    1    6
 9:    D2    2    1    8
10: meanA    3    8   12
11: meanB    8    3    6
12: meanC    7    3   10
13: meanD    3    1    7

Data

df1 <- structure(list(col1 = c(4L, 2L, 3L, 10L, 6L, 12L, 2L, 4L, 2L), 
    col2 = c(11L, 9L, 4L, 5L, 1L, 1L, 5L, 1L, 1L), col3 = c(15L, 
    17L, 4L, 4L, 8L, 12L, 8L, 6L, 8L)), class = "data.frame", row.names = c("A1", 
"A2", "A3", "B1", "B2", "C1", "C2", "D1", "D2"))

df2 <- structure(list(meancol1 = c(3L, 8L, 7L, 3L), meancol2 = c(8L, 
3L, 3L, 1L), meancol3 = c(12L, 6L, 10L, 7L)), class = "data.frame", row.names = c("meanA", 
"meanB", "meanC", "meanD"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.