0

I want to combine two data frames but the order is different and some colums are missing in RR.

Here is an example:


df.1 <- data.frame(class = c(1,6,8,9,7,8,9,6,4), math = c(0.7, 0.4, 0.7), hist = c(0.6, 0.4, 0.3), geom = c(0.7, 0.4, 0.7), eng = c(0.7, 0.4, 0.7), draw = c(0.8, 0.6, 0.7))

df.2 <- data.frame(eng = c(0.7, 0.4, 0.7, class = c(2, 1, 2, 3, 1),draw = c(0.8, 0.6, 0.7),geom = c(0.7, 0.4, 0.7) )

Thank you

New Dev
  • 48,427
  • 12
  • 87
  • 129
Béa
  • 101
  • 2
  • 9
  • Thank you for your answer. I don't want to cbind the data. I want to merge it but the order is different and some columns are missing. sorry for my bad english... – Béa Jul 11 '20 at 10:01
  • You cannot combine if both `data.frames` (vertically: adding more rows) in R do not have the same number of columns, same names and same data.type. Order does not matter. Check `rbind()`. – tushaR Jul 11 '20 at 10:02
  • I used merge function but it does not work – Béa Jul 11 '20 at 10:02
  • You need something - an extra column in each data frame, for example - to define the order in which you want to join the two data frames. Then use some form of join. Wthout the extra column, an id column, R doesn't have the information it needs to do what you want. It would help to know what your desired result, based on your sample data, would be. – Limey Jul 11 '20 at 10:03
  • `df.2` is not a properly formatted data frame, could you edit it please so that it is a valid object? – Peter Jul 11 '20 at 10:05

2 Answers2

1

Is this what you are looking for?

library(dplyr)

df <- 
  full_join(df.1, df.2)
#> Joining, by = c("class", "geom", "eng", "draw")

df
#>    class math hist geom eng draw
#> 1      1  0.7  0.6  0.7 0.7  0.8
#> 2      6  0.4  0.4  0.4 0.4  0.6
#> 3      8  0.7  0.3  0.7 0.7  0.7
#> 4      9  0.7  0.6  0.7 0.7  0.8
#> 5      7  0.4  0.4  0.4 0.4  0.6
#> 6      8  0.7  0.3  0.7 0.7  0.7
#> 7      9  0.7  0.6  0.7 0.7  0.8
#> 8      6  0.4  0.4  0.4 0.4  0.6
#> 9      4  0.7  0.3  0.7 0.7  0.7
#> 10     2   NA   NA  0.7 0.7  0.8
#> 11     1   NA   NA  0.4 0.4  0.6
#> 12     2   NA   NA  0.7 0.7  0.7
#> 13     3   NA   NA  0.7 0.7  0.8
#> 14     1   NA   NA  0.4 0.4  0.6
#> 15     3   NA   NA  0.7 0.7  0.7

Created on 2020-07-11 by the reprex package (v0.3.0)

data

df.1 <- data.frame(class = c(1,6,8,9,7,8,9,6,4), 
                   math = c(0.7, 0.4, 0.7), 
                   hist = c(0.6, 0.4, 0.3), 
                   geom = c(0.7, 0.4, 0.7), 
                   eng = c(0.7, 0.4, 0.7), 
                   draw = c(0.8, 0.6, 0.7))

df.2 <- data.frame(eng = c(0.7, 0.4, 0.7), 
                   class = c(2, 1, 2, 3, 1, 3),
                   draw = c(0.8, 0.6, 0.7),
                   geom = c(0.7, 0.4, 0.7) )

Peter
  • 11,500
  • 5
  • 21
  • 31
0

the following is all I can get

class = c(1, 6, 8, 9, 7, 8, 9, 6, 4)
math = c(0.7, 0.4, 0.7)
hist = c(0.6, 0.4, 0.3)
geom = c(0.7, 0.4, 0.7)
eng = c(0.7, 0.4, 0.7)
draw = c(0.8, 0.6, 0.7)

dimension <-
  seq(max(
    length(class),
    length(math),
    length(hist),
    length(geom),
    length(eng),
    length(draw)
  ))

df.new <- data.frame(class[dimension], math[dimension], hist[dimension], 
                     geom[dimension], eng[dimension], draw[dimension])

names(df.new) <- c('class', 'math', 'hist', 'geom', 'eng', 'draw')

Output

df.new
  class math hist geom eng draw
1     1  0.7  0.6  0.7 0.7  0.8
2     6  0.4  0.4  0.4 0.4  0.6
3     8  0.7  0.3  0.7 0.7  0.7
4     9   NA   NA   NA  NA   NA
5     7   NA   NA   NA  NA   NA
6     8   NA   NA   NA  NA   NA
7     9   NA   NA   NA  NA   NA
8     6   NA   NA   NA  NA   NA
9     4   NA   NA   NA  NA   NA
odunayo12
  • 425
  • 5
  • 10