How to integrate many vectors into several data.frame into one without duplication?

I have a position index vector in data.frame objects, but in each data.frame object, the order of the position index vector is very different. However, I want to integrate / merge the object object.frame object into one common data.frame file with a very specific order and avoid duplication in it. Does anyone know of any trick to make this easier? Can anyone suggest a possible approach how to accomplish this task?

data

v1 <- data.frame(
  foo=c(1,2,3),
  bar=c(1,2,2),
  bleh=c(1,3,0))

v2 <-  data.frame(
  bar=c(1,2,3),
  foo=c(1,2,0),
  bleh=c(3,3,4))

v3 <-  data.frame(
  bleh=c(1,2,3,4),
  foo=c(1,1,2,0),
  bar=c(0,1,2,3))

initial result after their integration:

initial_output <- data.frame(
  foo=c(1,2,3,1,2,0,1,1,2,0),
  bar=c(1,2,2,1,2,3,0,1,2,3),
  bleh=c(1,3,0,3,3,4,1,2,3,4)
)

remove duplication

rmDuplicate_output <- data.frame(
  foo=c(1,2,3,1,0,1,1),
  bar=c(1,2,2,1,3,0,1),
  bleh=c(1,3,0,3,4,1,2)
)

final desired result:

final_output <- data.frame(
  foo=c(1,1,1,1,2,3,0),
  bar=c(0,1,1,1,2,2,3),
  bleh=c(1,1,2,3,3,0,4)
)

How can I get my final desired result? Is there an efficient way for this kind of manipulation for a data.frame object? Thanks

+4
3

bind_rows dplyr, distinct arrange 'bar'

library(dplyr)
bind_rows(v1, v2, v3) %>%
             distinct %>%
             arrange(bar)
#    foo bar bleh
#1   1   0    1
#2   1   1    1
#3   1   1    3
#4   1   1    2
#5   2   2    3
#6   3   2    0
#7   0   3    4
+5

:

# combine dataframes
df = rbind(v1, v2, v3)

# remove duplicated
df = df[! duplicated(df),]

# sort by 'bar' column
df[order(df$bar),]
    foo bar bleh
7   1   0    1
1   1   1    1
4   1   1    3
8   1   1    2
2   2   2    3
3   3   2    0
6   0   3    4
+4

mget/ls, ( ), data.table rbindlist unique (. )

library(data.table)
unique(rbindlist(mget(ls(pattern = "v\\d+")), use.names = TRUE))
#    foo bar bleh
# 1:   1   1    1
# 2:   2   2    3
# 3:   3   2    0
# 4:   1   1    3
# 5:   0   3    4
# 6:   1   0    1
# 7:   1   1    2

data.frame ,

+4
source

Source: https://habr.com/ru/post/1651053/


All Articles